简体   繁体   中英

mysql_fetch_array won't loop through all results

First off, I know we have to get off of the depreciated php mysql functions and move to mysqli or PDO. However, that transition won't be happening here for a few weeks and I need to get this working like ASAP.

Basically, I have code that works fine on our old server (PHP 5.2.13), as well as smaller queries on our new server (PHP 5.4.20), but for larger queries will only return a partial record set and then just... die I guess? What record it dies on depends on the query, but it pretty much always dies somewhere in the range of record 10k to 15k. I suspect it is dying because of some kind of php.ini setting that sets a limit or something but I have no idea what it would be. I've streamlined the code to the essentials here:

$query = $my_query;
$result = mysql_query($query) or die(mysql_error());
$record_count = mysql_num_rows($result);

echo "Query has returned " . $record_count . " records.<br>";

$y=0;
while($row=mysql_fetch_array($result, MYSQL_ASSOC))
{
    echo "START";
    echo $y . " ";
    foreach($row as $key => $value)
    { echo $value . " "; }
    $y=$y+1;

    echo "END" . "<br>";
}
echo "GOT OUT OF THERE!";

So yeah, the record_count will echo that the query returned about 250k records, but in the loop it basically will do somewhere between 10-15k records, echo the final "END", but then the loop just plain stops. It doesn't get back to the next "START" nor does it ever get to the "GOT OUT OF THERE!" And again, this same exact code works fine on our old server, as well as smaller queries on our new server.

Anyone have any ideas what the issue is?

It's probably just timing out. You can override the server's default timeout settings for an individual script by adding this line:

set_time_limit(0);

This will allow the script to run forever. If you want to set a different time limit, the parameter is in seconds, so for instance, this will allow the script to run for 5 minutes:

set_time_limit(300);

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