Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to debug a newsletter mailing script for a project I am working on. This used to work perfectly fine with PHP Mailer, however I ended up changing my mailer to Swift Mailer and since I have been having the most bizarre problem.

I keep getting the following warning, after the first iteration of the while loop (one e-mail is sent out to the first e-mail address in the mysql result set, then I get this warning):

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, integer given in /home1/username/public_html/domain/newslettermailer.php on line 15

This is my newlettrmailer.php script:

   include('inc/connect.inc.php');

    $query = "SELECT * FROM `newsletter`";

    $result = mysqli_query($mysqli, $query);

    $status = Array();

    require('mailer/swift_required.php');


    while($row = mysqli_fetch_array($result)){
        $email =  $row['email'];

        echo $email . " GOOD<br/>";

      // Create the Transport
      $transport = Swift_SmtpTransport::newInstance('mail.domain.com', 25)
      ->setUsername('[email protected]')
      ->setPassword('password');

      // Create the Mailer using your created Transport
        $mailer = Swift_Mailer::newInstance($transport);

        $mailTo = Array();
        $mailTo[] = $email;  


    $message = Swift_Message::newInstance('Newsletter Test!')
      ->setFrom(array('[email protected]' => 'Subject'))
      ->setTo($mailTo);

    $body = '<strong style="text-decoration:underline;">Test</strong>';
      $message->setBody($body,'text/html');


          // Send the message
        $result = $mailer->send($message);



        if($result==0)
        { //MESSAGE FAILED
            $status .= $email . ': <span style="color:red; font-weight:bold;">Fail</span><br/>';
        }
        else
        { //SUCCESS!
            $status .= $email . ': <span style="color:green; font-weight:bold;">Success!</span><br/>';
        }

    }

While trying to debug, I modified the script above to the following, and got not errors and no warnings what so ever, and all e-mails printed out:

         include('inc/connect.inc.php');

        $query = "SELECT * FROM `newsletter`";

        $result = mysqli_query($mysqli, $query);

        $status = Array();

        require('mailer/swift_required.php');


        while($row = mysqli_fetch_array($result)){
            echo $row['email'] . '<br/>';

        }

I have been trying to debug this for quite a while now and am really having a difficult time with this, I appreciate any suggestions as to why this is happening.

Many thanks in advance!

share|improve this question

closed as too localized by Barmar, andrewsi, Edwin Alex, Soner Gönül, syb0rg May 16 at 6:00

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

1 Answer

up vote 7 down vote accepted

You're reusing the variable $result:

$result = $mailer->send($message);

So on the second iteration of the while loop, it's trying to pass that to mysqli_fetch_array() instead of the result return from mysqli_query().

Use a different variable for this second result.

share|improve this answer
+1, Thanks for the reply. I appreciate it! This indeed was the problem :) – AnchovyLegend May 15 at 18:55
Since the problem happens on the 2nd iteration, the obvious thing to look for is an assignment to the variable within the loop, isn't it? – Barmar May 15 at 19:03
Wasn't so obvious for me, although it should of been, you're right. For some reason I thought it was mysql related. – AnchovyLegend May 15 at 19:17
That's why you should read error messages carefully. This one tells you exactly what went wrong -- somehow $result changed from being a mysqli_result to an integer. And since it was OK on the first iteration, mysqli_query must have been successful. – Barmar May 15 at 19:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.