views:

60

answers:

5

Hey ppl..

i wrote a code to update the password encryption 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?

+1  A: 

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?

roman
thanks.. but my max_execution_time is abt 500secs.. i edited it.. still the error shows up.. any idea abt the error?
dineshbabu
did you restart your webserver? :)
roman
yup.. btw i queried it directly using ur command in phpmyadmin.. it took jus 0.013 secs.. thanks a lot..!! oh im such a n00b.. :(
dineshbabu
last addition: i hope you change your salt after posting it on the innertubes :)
roman
+1  A: 
<?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);

?> 
code_burgar
A: 

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.

Aistina
A: 

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
Sarfraz
A: 

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");
Erik