开发者

Fatal error in php (exceeded 60s of exec time)

i wrote a code to update the password encry开发者_StackOverflowption in my site.. the code is..

<?php
$db= mysql_pconnect("localhost","root","root");
mysql_select_db("nitconnect");
$query="select password from register limit 1000";
$result=mysql_query($query);
$c=0;
while($ans=mysql_fetch_array($result))
{
    $newpass=sha1($ans[0].'Q*iV%qKz$&!C');
    $newone="update register set password='{$newpass}' where password='{$ans[0]}'";
    mysql_query($newone);
}
?>

How do i correct this error?


there is no real solution other than setting php's max_execution_time higher in php.ini but:

mysql natively supports sha1, so maybe you can just do something like

update register set password = sha1(concat(password, 'Q*iV%qKz$&!C'))

this should be a lot quicker than your php solution.

still the performance of your query looks very low unless you are on an ancient box. maybe you have no index defined for the password field?


<?php 

  $db= mysql_pconnect("localhost","root","root"); 
  mysql_select_db("nitconnect"); 
  $query="UPDATE register SET password = SHA1(CONCAT(password, 'Q*iV%qKz$&!C')) WHERE 1"; 
  $result=mysql_query($query);

?> 


Assuming MySQL's native SHA1 method (as roman suggested) doesn't work, try adding this into your loop:

set_time_limit(10);

It basically will reset the PHP timeout everytime that function is called, giving it 10 more seconds to complete.


You are selecting 1000 records and then using each value in foreach loop and updating the db again, this is probably what makes you out of the time, try increasing the time limit by putting this on top of your script:

ini_set('max_execution_time', 14000); // or whatever value


Another solution would be to do records in chunks, then use a header to send yourself back to the same script, passing the last updated record's index, and then do the next chunk.

Something like:

if(issset($_GET['start']) {
   $start = $_GET['start'];
} else {
   $start = 0;
}

$query="select password from register limit $start, 50";
... do your stuff ...

$start = $start + 50;
header("Location: http://www.example.com/yourscript.php?start=$start");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜