PHP:不能多次遍历mysql行

Gro*_*ler 5 php mysql

我正在进行分页,由于某种原因,我不能使用mysql_fetch_array多次循环结果.

//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
Run Code Online (Sandbox Code Playgroud)

我应该使用不同的PHP函数来获取数据库中的行数据吗?

谢谢!

Sta*_*ble 8

这是错误

while ($row = mysql_fetch_array($result)) {
   $total_images++;
}
Run Code Online (Sandbox Code Playgroud)

一旦你获取了数组,数组指针就会设置到最后.

用这个

 $total_images=mysql_num_rows($result);
Run Code Online (Sandbox Code Playgroud)

$images_to_offset=mysql_num_rows($result);
Run Code Online (Sandbox Code Playgroud)

要么

要重置指针的位置使用mysql_data_seek().它移动内部结果指针