Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-13-2012, 09:32 AM   PM User | #1
jeddi
Senior Coder

 
Join Date: May 2006
Posts: 1,513
Thanks: 26
Thanked 4 Times in 4 Posts
jeddi has a little shameless behaviour in the past
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 == ) {
    
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

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.
jeddi is offline   Reply With Quote
Old 04-13-2012, 10:14 AM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Quote:
$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?
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 04-13-2012, 12:06 PM   PM User | #3
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by jeddi View Post
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.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//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. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 04-13-2012, 01:52 PM   PM User | #4
jeddi
Senior Coder

 
Join Date: May 2006
Posts: 1,513
Thanks: 26
Thanked 4 Times in 4 Posts
jeddi has a little shameless behaviour in the past
OK

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 == ) {
    
write_log("No Valid Campaigns");
    } 
// end if

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

And I get this output:

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

Last edited by jeddi; 04-13-2012 at 01:58 PM..
jeddi is offline   Reply With Quote
Old 04-13-2012, 03:35 PM   PM User | #5
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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()); 
to this:
PHP Code:
if ($result mysql_query($sql))
   {
   print 
mysql_num_rows($result);
   }
else
   {
   print 
mysql_error();
   } 
Report back with your findings.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//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. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 04-13-2012, 05:17 PM   PM User | #6
jeddi
Senior Coder

 
Join Date: May 2006
Posts: 1,513
Thanks: 26
Thanked 4 Times in 4 Posts
jeddi has a little shameless behaviour in the past
OK I did what you suggested:


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

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 == ) {
    
write_log("No Valid Campaigns");
    } 
// end if

else {    
    
    while(
$row mysql_fetch_assoc($result)){
    
extract($row); 
Quote:
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.
jeddi is offline   Reply With Quote
Old 04-13-2012, 06:26 PM   PM User | #7
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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 wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//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. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 04-13-2012, 07:36 PM   PM User | #8
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Ok, well an hour has gone now so you'll have to arrange a time with me by PM instead.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//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. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 04-13-2012, 08:16 PM   PM User | #9
jeddi
Senior Coder

 
Join Date: May 2006
Posts: 1,513
Thanks: 26
Thanked 4 Times in 4 Posts
jeddi has a little shameless behaviour in the past
Hi - no I don't have Teamviewer


I just thought I'd add something extra:

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

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

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 == ) {
    
write_log("No Valid Campaigns");
    } 
// end if

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

The output:


Quote:
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.
jeddi is offline   Reply With Quote
Old 04-13-2012, 08:29 PM   PM User | #10
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
PHP Code:
    $i 0;
    while(++
$i && ($row mysql_fetch_assoc($result))){
    
printf("Iteration: %d" PHP_EOL$i);
    
extract($row); 
Does that alter the output?
Fou-Lu is offline   Reply With Quote
Old 04-13-2012, 10:16 PM   PM User | #11
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,452
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Can you show the complete loop that starts with

PHP Code:
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.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 04-14-2012, 01:30 AM   PM User | #12
litebearer
Regular Coder

 
Join Date: Apr 2004
Posts: 287
Thanks: 0
Thanked 21 Times in 21 Posts
litebearer is on a distinguished road
Re: extract, see http://php.net/manual/en/function.extract.php the section by darylblake at gmail dot com 12-Apr-2011 05:07
litebearer is offline   Reply With Quote
Old 04-14-2012, 03:01 AM   PM User | #13
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,452
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by litebearer View Post
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.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 04-14-2012, 11:45 AM   PM User | #14
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//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. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 04-14-2012, 01:07 PM   PM User | #15
jeddi
Senior Coder

 
Join Date: May 2006
Posts: 1,513
Thanks: 26
Thanked 4 Times in 4 Posts
jeddi has a little shameless behaviour in the past
Hello,

Thanks for all the replies.

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.
jeddi is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:12 AM.


Advertisement
Log in to turn off these ads.