...

View Full Version : Script works but get a warning: mysql_fetch_assoc() expects parameter 1 to be resourc



jeddi
04-13-2012, 09:32 AM
Hi,

I have written a script to build a queue for emails.

It is working but I get a warning:



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:


$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

else {
echo "<br>$sql<br>";
while($row = mysql_fetch_assoc($result)){
extract($row);



Line 164 is :
while($row = mysql_fetch_assoc($result)){

I guess my query contains an error but I just can not see where :confused:

Does it contain some "reserved words" or something ?

Any help, much appreciated.

Thanks



.

abduraooft
04-13-2012, 10:14 AM
$result = mysql_query($sql) or write_error("Could not do Broadcast Query 1 ".mysql_error()." \r\n");
What's write_error() ? What's its outcome?

tangoforce
04-13-2012, 12:06 PM
This is my code:

[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.

jeddi
04-13-2012, 01:52 PM
OK

I got rid of the function write_error() and replaced it with a "die"

So I now have:


$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

else {

while($row = mysql_fetch_assoc($result)){
extract($row);



And I get this output:


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 ??


.

tangoforce
04-13-2012, 03:35 PM
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:


$result = mysql_query($sql) or die("Could not do emailqueue 2: ".mysql_error());


to this:


if ($result = mysql_query($sql))
{
print mysql_num_rows($result);
}
else
{
print mysql_error();
}


Report back with your findings.

jeddi
04-13-2012, 05:17 PM
OK I did what you suggested:



$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>";

if ($result = mysql_query($sql))
{
print mysql_num_rows($result);
}
else
{
print mysql_error();
}

$num = mysql_num_rows($result);

echo "<br>$num<br>";

if ($num == 0 ) {
write_log("No Valid Campaigns");
} // end if

else {

while($row = mysql_fetch_assoc($result)){
extract($row);




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.


.

tangoforce
04-13-2012, 06:26 PM
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.

tangoforce
04-13-2012, 07:36 PM
Ok, well an hour has gone now so you'll have to arrange a time with me by PM instead.

jeddi
04-13-2012, 08:16 PM
Hi - no I don't have Teamviewer


I just thought I'd add something extra:

I added: echo "<br>$result<br>";



$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>";

if ($result = mysql_query($sql))
{
print mysql_num_rows($result);
}
else
{
print mysql_error();
}

echo "<br>$result<br>";

$num = mysql_num_rows($result);

echo "<br>$num<br>";

if ($num == 0 ) {
write_log("No Valid Campaigns");
} // end if

else {

while($row = mysql_fetch_assoc($result)){
extract($row);





The output:




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




.

Fou-Lu
04-13-2012, 08:29 PM
$i = 0;
while(++$i && ($row = mysql_fetch_assoc($result))){
printf("Iteration: %d" . PHP_EOL, $i);
extract($row);

Does that alter the output?

felgall
04-13-2012, 10:16 PM
Can you show the complete loop that starts with


while($row = mysql_fetch_assoc($result)){
extract($row);

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.

litebearer
04-14-2012, 01:30 AM
Re: extract, see http://php.net/manual/en/function.extract.php the section by darylblake at gmail dot com 12-Apr-2011 05:07

felgall
04-14-2012, 03:01 AM
Re: extract, see http://php.net/manual/en/function.extract.php the section by darylblake at gmail dot com 12-Apr-2011 05:07

Obviously you have a typo somewhere - we need to see your actual code not the code you have miscopied.

tangoforce
04-14-2012, 11:45 AM
Seems the op has a preference for going quiet on us now the weekend is here.

The op also hasn't mentioned if its the first loop thats causing this or the 2nd/3rd etc which is rather important.

jeddi
04-14-2012, 01:07 PM
Hello,

Thanks for all the replies.

My "going quite" is due to needing to sleep occasionally :eek:

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. :thumbsup:

Thanks again

tangoforce
04-14-2012, 01:21 PM
Now - some great points made about the value of $result
possibly changing etc.

I'll take a look and report back. :thumbsup:


So by that do you mean that the code you originally posted is not the same as you are actually trying to fix? - IE there is more to it? IF so you've wasted quite a few peoples time. Thats why you should always post your code as-is not editing out the bits that you THINK we don't need to see.

Fou-Lu
04-14-2012, 06:00 PM
Yes and no. I think that the OP has seen that the problem might not necessarily be before the loop, I don't think that they mean something has drastically changed at this point. One thing I did notice is that the error lines are very different. The first one was nested about 150 lines into script, and the following around 50. Not sure if that was from a document structure change, or if the error is a new one.

The code I posted was also to see if it was caused by the loop. If it successfully iterates at least one record, then the cause is within the loop. I would love to pin it on extract since IMO its a very bad idea to use this function, but it doesn't actually appear to be the cause in this situation given the available columns of the query. So this would point to a manual assignment to $result within the loop to a boolean value, possibly to an INSERT, UPDATE or DELETE (as $result is often used as a meaning to assign resultsets from queries, but could be a cause of a calculation as well). Another cause could be by use of a global variable pinned into that of a function call which often use the symbol $result as well. Which is just another reason to use global very sparingly.

tangoforce
04-14-2012, 06:17 PM
Yes I noticed the line number changes too but the op seemed insistant it was the same line of code.

As you say though if $result is being changed within the loop then its either down to extract OR the op has other code in there that they've not shown us.

Oh well..

jeddi
04-16-2012, 07:55 PM
Thanks again for your help.

The comment


later in that loop that is changing the value of $result

made me look through the code more closely and I found that I was changing the value of the $result without realizing it.

So ... problem solved :thumbsup:


.

tangoforce
04-16-2012, 09:26 PM
So you DID have other code in there then? - In other words the code you originally posted for us to look at was just posted to waste our time.

Please don't do that again. Next time post your ACTUAL code.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum