1

The table is about 1.2M rows (its actively going up, so total rows grows slightly between iterations of this test, but that doesn't change the outcome).

mysql_num_rows is displayed - which is accurate, tested with select count(*) - after the pull query in order to show how large the result set is.

Then, a loop assigns the row to an array with mysql_fetch_array(). Iteration counter is incremented for every round, and you would expect it go get to mysql_num_rows and then break out of the while loop. Pretty standard stuff, been done a million times over.

What actually happens, on the other hand, is quite odd. It gets to exactly 1/2 way (floor(number_of_rows/2)) and then mysql_fetch_array() returns false. No matter how you limit the result set...

$iteration = 0;
$result = mysql_query("select `file_id`, `size` from `files`", $dbconn); // get all records
echo "\nDone. Found " . mysql_num_rows($result) . " rows."; // Done. Found 1291595 rows.
if ($result){
    while ($line = mysql_fetch_array($result) !== false){
         $iteration++;
    }
    echo "\ngot to $iteration before mysql_fetch_array was false."; // got to 642679 before mysql_fetch_array was false.
}

Sometimes $line is an empty array, sometimes mysql_fetch_array triggers false.

It gets mysql_num_rows that i'd expect and continues for 1/2 the records of the total result set size, then it stops...

If I put now, limit = 967356,1000000, I get:

Done. Found 324963 rows.
got to 162482 before mysql_fetch_array was false.

This is 1 off from being exactly 1/2 way.

limit = 1000000, 1000000:

Done. Found 292606 rows.
got to 146303 before mysql_fetch_array was false.

What in the world could this be?

php info:

php -v PHP 5.4.19-1+debphp.org~precise+3 (cli) (built: Aug 27 2013 14:29:42) Copyright (c) 1997-2013 The PHP Group

4

2 回答 2

1

对不起各位,我太笨了。我在 while 循环中有第二个 $line=mysql_fetch_array($result) ......导致它为每个循环抓取 2 行。道歉。

于 2013-10-18T21:31:34.013 回答
1

为了避免在 php 将值解释$line为 false 的情况下提前终止 while 循环,请更改行

while ($line = mysql_fetch_array($result)){

while (($line = mysql_fetch_array($result)) !== FALSE){

更新:

另一种可能性是您在非常大的结果集上内存不足。尝试使用mysql_unbuffered_query而不是mysql_query().

另一个更新:

另一种可能性是您在此脚本中打开了多个数据库连接,并且mysql_query()没有使用您认为正在使用的数据库连接。传递可选的第二个参数以确保。改变

mysql_fetch_array($result)

mysql_fetch_array($result, $dbconn)
于 2013-10-18T21:19:11.213 回答