Jump to content

Archived

This topic is now archived and is closed to further replies.

jaymc

Only 1 Fetch?

Recommended Posts

Here is my code

[code]$querya = "SELECT * FROM `messages` WHERE `TO` = '$User_Session' ORDER BY `DATE` DESC LIMIT 0,10";

$runquerya = mysql_query($querya);[/code]

Now, I am using a while loop in conjuction with a mysql array fetch to retrieve the rows, like so..


[code]while ($resultsa = mysql_fetch_array($runquerya)) {
echo $resultsa[FROM];
}[/code]

That works fine, however, if I then run the same loop again in the same script, it wont work. like so..

[code]while ($resultsb = mysql_fetch_array($runquerya)) {
echo $resultsb[FROM];
}[/code]

The only way I can get it to work is if I run the same mysql_query() twice but store the results in different variables, likes so..

[code]$querya = "SELECT * FROM `messages` WHERE `TO` = '$User_Session' ORDER BY `DATE` DESC LIMIT 0,10";

$runquerya = mysql_query($querya);
$runqueryb = mysql_query($querya);[/code]

And then do a mysql_fetch_array for the first, then the second. That works. But obviously Its not the correct way to go around it. Im just wondering why this is happening? Its as if once running a mysql_fetch_array() its emptying out $runquerya meaning it cant be used again?

Completely lost... any ideas?

Share this post


Link to post
Share on other sites
you have an error in your syntax..

[code=php:0]$resultsb['FROM'][/code]

Share this post


Link to post
Share on other sites
It still works though... without the literals

But the problem explained about happens with or without the ' '

Share this post


Link to post
Share on other sites
This is because there's an internal pointer that gets moved on each time a row is accessed.  So if you use a while loop to access all the rows, the next time you try to get a row (by creating another while loop) the pointer is at the end of the result set.

Try adding this line after the first while loop:

[code=php:0]mysql_data_seek($runquerya, 0);
[/code]

Regards
Huggie

Share this post


Link to post
Share on other sites
[quote author=jaymc link=topic=112511.msg456709#msg456709 date=1161686476]
It still works though... without the literals

But the problem explained about happens with or without the ' '
[/quote]The solution has been explained above, but in regards to it working "fine" without the literals, that's not entirely true. I can tell just from the post I've quoted you do not have error reporting set to include E_NOTICE, else you will see many, many error messages.

Share this post


Link to post
Share on other sites
[quote author=HuggieBear link=topic=112511.msg456710#msg456710 date=1161686494]
This is because there's an internal pointer that gets moved on each time a row is accessed.  So if you use a while loop to access all the rows, the next time you try to get a row (by creating another while loop) the pointer is at the end of the result set.

Try adding this line after the first while loop:

[code=php:0]mysql_data_seek($runquerya, 0);
[/code]

Regards
Huggie
[/quote]

That worked a treat! Thanks!!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.