944,052 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 27798
  • PHP RSS
Jul 8th, 2005
0

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

Expand Post »
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!
Reputation Points: 11
Solved Threads: 0
Newbie Poster
bgharper is offline Offline
12 posts
since Jul 2005
Jul 8th, 2005
0

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

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]
Reputation Points: 36
Solved Threads: 6
Posting Whiz
Troy is offline Offline
354 posts
since Jun 2005
Jul 8th, 2005
0

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

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...
Reputation Points: 11
Solved Threads: 0
Newbie Poster
bgharper is offline Offline
12 posts
since Jul 2005
Jul 8th, 2005
0

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

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.
Reputation Points: 36
Solved Threads: 6
Posting Whiz
Troy is offline Offline
354 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: question about connecting odbc to sql through php script
Next Thread in PHP Forum Timeline: Tidy up output of ping.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC