while function and "mysql_fetch_assoc() not a valid MySQL result resource"

Reply

Join Date: Jul 2005
Posts: 12
Reputation: bgharper is an unknown quantity at this point 
Solved Threads: 0
bgharper bgharper is offline Offline
Newbie Poster

while function and "mysql_fetch_assoc() not a valid MySQL result resource"

 
0
  #1
Jul 8th, 2005
Hi all. I am having a problem I've not encountered before. When using mysql_fetch_assoc() in while(), I am getting the following error:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in archives.php on line 81
However, when taking it out of the while(), it works fine. The following is the code snippet...

[PHP]$query = "SELECT * FROM comments WHERE archive_id = '$archive_id'";
$result = mysql_query($query);

while ( $result = mysql_fetch_assoc($result) ) {

$date = $result["date"];
$name = $result["name"];
$comments = $result["comments"];

echo "$date, $name, $comments";

}[/PHP]I have checked database connections, I've run the query through phpMyAdmin, and checked for spelling/syntax errors.

Any ideas? Thanks!
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 354
Reputation: Troy is an unknown quantity at this point 
Solved Threads: 5
Troy's Avatar
Troy Troy is offline Offline
Posting Whiz

Re: while function and "mysql_fetch_assoc() not a valid MySQL result resource"

 
0
  #2
Jul 8th, 2005
Try this:
[PHP]
$query = "SELECT * FROM comments WHERE archive_id = '$archive_id'";
echo $query;
exit();
$res = mysql_query($query);
while ( $result = mysql_fetch_assoc($res) ) {
$date = $result["date"];
$name = $result["name"];
$comments = $result["comments"];
echo "$date, $name, $comments";
}[/PHP]
Troy Wolf is the author of SnippetEdit. "Website editing as easy as it gets." IX Web Hosting
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 12
Reputation: bgharper is an unknown quantity at this point 
Solved Threads: 0
bgharper bgharper is offline Offline
Newbie Poster

Re: while function and "mysql_fetch_assoc() not a valid MySQL result resource"

 
0
  #3
Jul 8th, 2005
I had to remove the exit(), but it worked fantastically. Why would changing $result to $res work differently than keeping $result? I'm calling result earlier in the page, however, it's never caused me problems before...
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 354
Reputation: Troy is an unknown quantity at this point 
Solved Threads: 5
Troy's Avatar
Troy Troy is offline Offline
Posting Whiz

Re: while function and "mysql_fetch_assoc() not a valid MySQL result resource"

 
0
  #4
Jul 8th, 2005
It will work in a single line statement, but not iteratively. This is because the very first time you issue the statement, you have swapped $result--the full result set for $result--the single row array. The next time through the loop, $result is the array--not the result set.
Troy Wolf is the author of SnippetEdit. "Website editing as easy as it gets." IX Web Hosting
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 16298 | Replies: 3
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC