Question

I have stripped this right down, to try and find the source of this issue, but can not pin point it at all.

This successfully echos each of the IDs

$result = mysqli_query($con,"SELECT id FROM users");

// Each user matching criteria
while($row = mysqli_fetch_array($result))
{   
    echo $row['id'];
}

However when I include Mandrill it stops working, however I will still get the first ID echo'd

// Search Database for users who signed up yesterday $result = mysqli_query($con,"SELECT id FROM users");

// Each user matching criteria
while($row = mysqli_fetch_array($result))
{   
    echo $row['id'];
  include($_SERVER['DOCUMENT_ROOT'].'/src/Mandrill.php');
  $mandrill = new Mandrill('hidden');

  try {
      $template_name = $content_template;
      $message = array(
          'html' => '<p>Example HTML content</p>',
          'text' => 'Example text content',
          'subject' => 'test',
          'from_email' => 'hidden',
          'from_name' => 'hidden',
          'to' => array(
              array(
                  'email' => 'hidden',
                  'name' => 'Tim',
                  'type' => 'to'
              )
          ),
          'headers' => array('Reply-To' => 'hidden.com'),
          'important' => false,
          'track_opens' => true,
          'track_clicks' => true,
          'tags' => array('$meta_tags'),
          'google_analytics_domains' => array('hidden'),
          'metadata' => array('website' => 'hidden')
      );
      $ip_pool = 'Main Pool';
      $result = $mandrill->messages->sendTemplate('jotText-Auto-i101-welcome', $template_content, $message, $async, $ip_pool, $send_at);
      print_r($result);
  } catch(Mandrill_Error $e) {
      // Mandrill errors are thrown as exceptions
      echo 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();
      // A mandrill error occurred: Mandrill_Unknown_Subaccount - No subaccount exists with the id 'customer-123'
      throw $e;
    }

}

The error message I get is

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, array given in /home/timbaker/public_html/jottext/foundation/database_tasks/cron_welcome.php on line 18

Which suggests to me it is a problem with the original query... But I can't work out why Mandrill causes this to happen?

Also worth noting: - No error messages from Mandril - Email does successfully send

Was it helpful?

Solution

Initially, $result variable stored only the result of mysqli_query invocation, and it worked quite fine. But when you've added the mail-sending code, you accidentally reused the same variable for storing the result of Mandrill action:

$result = $mandrill->messages->sendTemplate(...);

...thus losing its initial value (of mysqli_result), and swapping it (a resource) with a new value (an array) - hence the error message you got.

Apparently, the easiest way out of this is renaming this variable:

$mailStatus = $mandrill->messages->sendTemplate(...);
print_r($mailStatus); // or do something else with it

The bottom line: try to avoid too generic names - eventually all $vars and $data and $result will bite you quite hard when you mix them. And I'm not even talking about readability here. )


As a sidenote (kudos to @Pierre for mentioning that), you'd better move instantiating of $mandrill variable outside of the loop - otherwise you'll have include working each time a new row is processed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top