简体   繁体   中英

PHP: cannot loop through mysql rows more than once

I'm working on pagination, and for some reason I cannot use mysql_fetch_array to loop through a result more than once.

//both $page and $imagesPerPage != 0
$offset = $page * $imagesPerPage
while ($row = mysql_fetch_array($result)) {
   $total_images++;
}
echo $total_images;
//echos correct amount
$row = null;


$images_to_offset = 0;
while ($row = mysql_fetch_array($result) && $images_to_offset < $offset) {
   $images_to_offset++;
}
echo $images_to_offset;
//echos 0... which is wrong

Should I be using a different php function to get row data in a database?

Thanks!

This is the error

while ($row = mysql_fetch_array($result)) {
   $total_images++;
}

once you fetched the array the array pointer set to the end.

use this

 $total_images=mysql_num_rows($result);

and

$images_to_offset=mysql_num_rows($result);

OR

To reset the position of pointer Use mysql_data_seek() . it moves internal result pointer

If you wish to start fetching from the beginning after you've already fetched, you'll need you use mysql_data_seek() .

Also, please note that the mysql line of functions have been deprecated , and the community is encouraging use instead of MySQLi or PDO_MySQL lines of functions.

You could point the pointer back to the first row with mysql_data_seek .

mysql_data_seek($result, 0); 

Also see: http://ca2.php.net/manual/en/function.mysql-data-seek.php

Try using mysql_num_rows() , that way you don't have to iterate the $result twice which is giving you error in the second loop as you have to reset the result pointer. So do it like this which only iterates once:

//both $page and $imagesPerPage != 0
$offset = $page * $imagesPerPage
$total_images = mysql_num_rows($result);
echo $total_images;
//echos correct amount
$row = null;


$images_to_offset = 0;
while ($row = mysql_fetch_array($result) && $images_to_offset < $offset) {
   $images_to_offset++;
}
echo $images_to_offset;

As a side note, you should try to migrate to MySQLi or PDO_MySQL to access mysql as the interface you are using is now deprecated, see the red box in http://es.php.net/manual/en/function.mysql-num-rows.php

You can only loop through the results array once, after that they effectively 'disappear'. the way to loop over the results multiple times is to store them into a new array during the first loop, then loop over the new array as many times as you want...

You have to 'rewind' the array, by using the mysql_data_seek function:

$offset = $page * $imagesPerPage
while ($row = mysql_fetch_array($result)) {
   $total_images++;
}
echo $total_images;
//echos correct amount
$row = null;

mysql_data_seek(); // <-- rewind to the beginning

$images_to_offset = 0;
while ($row = mysql_fetch_array($result) && $images_to_offset < $offset) {
   $images_to_offset++;
}
echo $images_to_offset;

mysql_fetch_array not only returns an array that corresponds to the fetched row, it also moves the internal data pointer ahead.

There are more than one way of dealing with this, the most obvious is to "park" your result in an array itself and then work from there. Or query 2 times. Or use mysql_data_seek . In your case, maybe mysql_num_rows is more appropriate, since your code indicates you only want to know how many rows you have to iterate through, and thats what this function is there for.

Whatever decide, keep in mind that use of the mysql extension is discouraged. Instead, the MySQLi or PDO_MySQL extension should be used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM