Jump to content

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?
Link to comment
https://forums.phpfreaks.com/topic/24912-only-1-fetch/
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
Link to comment
https://forums.phpfreaks.com/topic/24912-only-1-fetch/#findComment-113550
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.
Link to comment
https://forums.phpfreaks.com/topic/24912-only-1-fetch/#findComment-113552
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!!
Link to comment
https://forums.phpfreaks.com/topic/24912-only-1-fetch/#findComment-113556
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.