3

Possible Duplicate:
PHP: mysql_fetch_array() expects parameter 1 to be resource, boolean given

I'm trying to send emails to addresses in a database. the addresses are in two parts: a username (aka 'number') and @something.com (aka 'carrier'). so the email address should be 'number' . 'carrier'

my code is below. the mail function works on its own (not in the loop). the error message i am getting is "mysql_fetch_array() expects parameter 1 to be resource, boolean given in home/username/public_html/script.php on line 15)

#!/usr/bin/php
<?
    require_once("PHPMailer/class.phpmailer.php");

    // connect to database
    mysql_connect("localhost", "username", "password");
    mysql_select_db("username_finalproject_Alerts");

    // prepare query
    $sql = "SELECT number, carrier FROM Alerts";
    // execute query
    $result = mysql_query($sql);
    while ($row = mysql_fetch_array($result))
    {
        //generate email address
        $email = $row['number'] . $row['carrier'];

        // send mail

        $mail = new PHPMailer();
        $mail->IsSMTP();
        $mail->Host = "private stmp address";
        $mail->ContentType = 'text/plain';
        $mail->IsHTML(false);
        $mail->SetFrom("private email address");
        $mail->AddAddress($email);
        $mail->Subject = "TRY 1";
        $mail->Body = "testing 1 2 3";
        if ($mail->Send() === false)
            die($mail->ErrorInfo . "\n");
    }

?>
Community
  • 1
  • 1
  • 2
    $result is empty and therefore you have this error. Probably you haven't mysql connection. Check mysql status and data connection. – Anthony Dec 08 '11 at 05:23

5 Answers5

3

There must be something wrong with your query, can you test it elsewere?

Perhaps you can try to catch the error

if(!$result) { echo mysql_error(); }

Insecurefarm
  • 391
  • 5
  • 16
0

Unless your login to the database is "username" and your password is "password" I think you probably meant

mysql_connect("localhost", $username, $password);

Not sure if this code is verbatim from yours or you just changed those for privacy but thought it might be worth pointing out.

TheOx
  • 2,208
  • 25
  • 28
0

to make easy you query you can also use: CONCAT(number, carrier) which provides you the combine result of both rows in a single result,

the second as the all suggested the use mysql_query($sql) or die('Error in Query!<br>' . mysql_error());

so that if the error in your query then you will get the error details through mysql_error()

jogesh_pi
  • 9,762
  • 4
  • 37
  • 65
0

check mysql_num_rows($rows)>0

    // connect to database
    mysql_connect("localhost", "username", "password");
    mysql_select_db("username_finalproject_Alerts");

    // prepare query
    $sql = "SELECT number, carrier FROM Alerts";
    // execute query
    $result = mysql_query($sql);
    if(mysql_num_rows($result)>0) {
    while ($row = mysql_fetch_array($result))
    {
        //generate email address
        $email = $row['number'] . $row['carrier'];

        // send mail

        $mail = new PHPMailer();
        $mail->IsSMTP();
        $mail->Host = "private stmp address";
        $mail->ContentType = 'text/plain';
        $mail->IsHTML(false);
        $mail->SetFrom("private email address");
        $mail->AddAddress($email);
        $mail->Subject = "TRY 1";
        $mail->Body = "testing 1 2 3";
        if ($mail->Send() === false)
            die($mail->ErrorInfo . "\n");
    }
}

?>
Mohit Bumb
  • 2,466
  • 5
  • 33
  • 52
-2

write like this

$email = $row[0] . $row[1];
Sonal Khunt
  • 1,876
  • 12
  • 20