Jump to content


Photo

Only 1 Fetch?


  • Please log in to reply
5 replies to this topic

#1 jaymc

jaymc
  • Members
  • PipPipPip
  • Advanced Member
  • 1,521 posts
  • LocationLiverpool

Posted 24 October 2006 - 10:28 AM

Here is my code

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

$runquerya = mysql_query($querya);

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


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

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

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

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

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

$runquerya = mysql_query($querya);
$runqueryb = mysql_query($querya);

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?
I would love to change the world, but they won't give me the source code

SEO Agency

#2 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 24 October 2006 - 10:33 AM

you have an error in your syntax..

$resultsb['FROM']


#3 jaymc

jaymc
  • Members
  • PipPipPip
  • Advanced Member
  • 1,521 posts
  • LocationLiverpool

Posted 24 October 2006 - 10:41 AM

It still works though... without the literals

But the problem explained about happens with or without the ' '
I would love to change the world, but they won't give me the source code

SEO Agency

#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 24 October 2006 - 10:41 AM

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:

mysql_data_seek($runquerya, 0);

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#5 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 24 October 2006 - 10:44 AM

It still works though... without the literals

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

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.

#6 jaymc

jaymc
  • Members
  • PipPipPip
  • Advanced Member
  • 1,521 posts
  • LocationLiverpool

Posted 24 October 2006 - 10:57 AM

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:

mysql_data_seek($runquerya, 0);

Regards
Huggie


That worked a treat! Thanks!!
I would love to change the world, but they won't give me the source code

SEO Agency




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users