Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update multiple rows or record in database

If I have a script like this:

$sql = "SELECT * FROM table WHERE Status='0'";
$query = mysql_query($sql);

    while ($row = mysql_fetch_array($query)) 
       $id = $row['ID'];

           //.........

       if($process->send()) { //after sent
         mysql_query("UPDATE table 
                      SET Status ='1' 
                      WHERE ID = '$id'"); 
       }
    }

So it will update each row when process is done.But if I have more than ten thousand records with the Status='0', the update will become slow .

So is there any better way to update the record? I can't update all with one single query since I need to know whether each process is done or not.

Thank you.

like image 216
Irene Ling Avatar asked Dec 07 '22 17:12

Irene Ling


1 Answers

Add all successful ones to an array and simply commit all of those simultaneously.

if ($process->send()) {
    $done[] = $id;
}

and a bit later:

mysql_query('UPDATE table SET Status=1 WHERE ID IN ('.implode(',', $done).')');
like image 74
Tom van der Woerdt Avatar answered Dec 10 '22 13:12

Tom van der Woerdt