Script works but get a warning: mysql_fetch_assoc() expects parameter 1 to be resourc
Hi,
I have written a script to build a queue for emails.
It is working but I get a warning:
Quote:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/exp526d/public_html/queue_create.php on line 164
This is my code:
PHP Code:
$sql = "SELECT broadcast_id, all_camps, camp_no, client_no, subject, message FROM broadcasts WHERE live = 'y' AND approve = 'y' AND queued = 'n' AND send_date <= now() ";
$result = mysql_query($sql) or write_error("Could not do Broadcast Query 1 ".mysql_error()." \r\n");
$num = mysql_num_rows($result);
if ($num == 0 ) {
write_log("No Valid Broadcasts");
} // end if
Line 164 is : while($row = mysql_fetch_assoc($result)){
I guess my query contains an error but I just can not see where
Does it contain some "reserved words" or something ?
Any help, much appreciated.
Thanks
.
__________________
If you want to attract and keep more clients, then offer great customer support.
Support-Focus.com. automates the process and gives you a trust seal to place on your website.
I recommend that you at least take the 30 day free trial.
[PHP]$sql = "SELECT broadcast_id, all_camps, camp_no, client_no, subject, message FROM broadcasts WHERE live = 'y' AND approve = 'y' AND queued = 'n' AND send_date <= now() ";
@jeddi: The message means that mysql didn't return a mysql result resource but instead returned false - IE because it couldn't understand your SQL. Either that or write_error() is using SQL that is wrong.
You need to run your SQL through phpmyadmin (click the table you want then the SQL tab at the top, paste it in, substitute PHP variables for real values and see what happens). You should get an error message from phpmyadmin indicating where the fault is in the SQL.
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.
I got rid of the function write_error() and replaced it with a "die"
So I now have:
PHP Code:
$sql = "SELECT camp_id, camps.client_no, author, mess_no, next_camp, sub_id, sub_name, sub_email, country, em_type FROM camps, subscriptions WHERE live = 'y' AND approve = 'y' AND confirm = 'y' AND queued = 'n' AND mess_no > '0' AND subscriptions.camp_no = camps.camp_id AND send_date <= now() ";
echo "<br>$sql<br>";
$result = mysql_query($sql) or die("Could not do emailqueue 2: ".mysql_error()); $num = mysql_num_rows($result);
echo "<br>$num<br>";
if ($num == 0 ) { write_log("No Valid Campaigns"); } // end if
SELECT camp_id, camps.client_no, author, mess_no, next_camp, sub_id, sub_name, sub_email, country, em_type FROM camps, subscriptions WHERE live = 'y' AND approve = 'y' AND confirm = 'y' AND queued = 'n' AND mess_no > '0' AND subscriptions.camp_no = camps.camp_id AND send_date <= now()
3
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/exp526d/public_html/queue_create.php on line 46
Line 46: while($row = mysql_fetch_assoc($result)){
So it finds 3 valid results but I get the warning !
Doesn't make sense .. or does it. ??
It appears that the query is OK because it doesn't Die.
I don't get "Could not do emailqueue 2: " and the mysql_error !!
So if there's no error ... why the warning ??
.
__________________
If you want to attract and keep more clients, then offer great customer support.
Support-Focus.com. automates the process and gives you a trust seal to place on your website.
I recommend that you at least take the 30 day free trial.
I personally never bother using 'or die()' in my scripts. I found it a bit tempermental. Instead I ise if/else. If i remember correctly, or die() only works with mysql_connect and mysql_select_db(). I have a vague memory of helping another person via TeamViewer with similar problems to this and I removed the or die() from his call to mysql_query().
The reason you are still getting this error ie because your mysql_query() call is returning a false result and it's passing that to mysql_fetch_array() which expects a resource.
There IS something wrong there somewhere but without seeing it in action it's difficult to advise more.
I want you to change this:
PHP Code:
$result = mysql_query($sql) or die("Could not do emailqueue 2: ".mysql_error());
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.
$sql = "SELECT camp_id, camps.client_no, author, mess_no, next_camp, sub_id, sub_name, sub_email, country, em_type FROM camps, subscriptions WHERE live = 'y' AND approve = 'y' AND confirm = 'y' AND queued = 'n' AND mess_no > '0' AND subscriptions.camp_no = camps.camp_id AND send_date <= now() ";
SELECT camp_id, camps.client_no, author, mess_no, next_camp, sub_id, sub_name, sub_email, country, em_type FROM camps, subscriptions WHERE live = 'y' AND approve = 'y' AND confirm = 'y' AND queued = 'n' AND mess_no > '0' AND subscriptions.camp_no = camps.camp_id AND send_date <= now()
5
5
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/exp526d/public_html/queue_create.php on line 54
And 54 is: while($row = mysql_fetch_assoc($result)){
Does this help ?
Thanks again for helping.
.
__________________
If you want to attract and keep more clients, then offer great customer support.
Support-Focus.com. automates the process and gives you a trust seal to place on your website.
I recommend that you at least take the 30 day free trial.
Yes / No. It helps as it's showing that something really screwy is going on but it doesn't help because its not executing the code block I'd expect (the print mysql_error() bit).
Do you have Teamviewer so I can connect to your desktop and see this for myself? If you do, please PM me your connection details - I WILL get to the bottom of this.
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.
$sql = "SELECT camp_id, camps.client_no, author, mess_no, next_camp, sub_id, sub_name, sub_email, country, em_type FROM camps, subscriptions WHERE live = 'y' AND approve = 'y' AND confirm = 'y' AND queued = 'n' AND mess_no > '0' AND subscriptions.camp_no = camps.camp_id AND send_date <= now() ";
SELECT camp_id, camps.client_no, author, mess_no, next_camp, sub_id, sub_name, sub_email, country, em_type FROM camps, subscriptions WHERE live = 'y' AND approve = 'y' AND confirm = 'y' AND queued = 'n' AND mess_no > '0' AND subscriptions.camp_no = camps.camp_id AND send_date <= now()
5
Resource id #5
5
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/exp526d/public_html/queue_create.php on line 56
So $result DOES contain a Resource !!!
So whats this stupid warning about then ??
Any know why this is happening ?
Thanks
.
__________________
If you want to attract and keep more clients, then offer great customer support.
Support-Focus.com. automates the process and gives you a trust seal to place on your website.
I recommend that you at least take the 30 day free trial.
It has to be something later in that loop that is changing the value of $result so that the second (or 5th) time that it returns to test the while condition the $result field no longer contains the resource.
The tests you have already done prove it contains a resource the first time it is tested but you haven't shown all the code it runs before the second test.
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.
My "going quite" is due to needing to sleep occasionally
We are in different time zones. I am presently in Turkey !
Now - some great points made about the value of $result
possibly changing etc.
I'll take a look and report back.
Thanks again
__________________
If you want to attract and keep more clients, then offer great customer support.
Support-Focus.com. automates the process and gives you a trust seal to place on your website.
I recommend that you at least take the 30 day free trial.