简体   繁体   中英

php mysql multi upade and internal server error

Ok, so I have a mysql table with over 130,000 rows.
What I am trying to do is "regenerate" the table. I have a column named bind which I want it to be the same as id(which is the index). For initial setup, the bind needs to be the same as id. Simple as that. The code used is below. I am using PDO, classes and functions.

$time_start = microtime(true); 
try{
    $sql = "SELECT * FROM `table_name`";
    $STH = $this->dbConn()->query($sql);
    $STH->execute();
    while($row = $STH->fetch(PDO::FETCH_OBJ)){
        if($row->id != $row->bind){
            $query = "UPDATE `table_name` SET `bind`=? WHERE `id`=?;";
            $STH2 = $this->dbConn()->prepare($query);
            $STH2->execute(array($row->id, $row->id));
        }
    }
} catch(PDOException $e) {
    $this->errorLog($e->getMessage());
}
$time_end = microtime(true);
$execution_time = ($time_end - $time_start)/60;
echo '<br /><b>Total Execution Time:</b> '.$execution_time.' Mins';

This is the part of code which I execute. $this->dbConn() is the object from my base class for db connection.

Problem 1. The update part takes too long to execute. However, If I only echo out the update statement it takes a few seconds.

Problem 2. I get a 500 Internal Server Error upon executing. Not a max_execution_time limit. The script is executing somehow in the background. I have reporting set to E_ALL, php.ini display_errors = On, and I can not find any error in the log files in /etc/logs/apache2 of /etc/php5-frm log file.

Solved but with some error. Maybe I am an idiot.

while($row = $STH->fetch(PDO::FETCH_OBJ)){
            if($row->id != $row->bind){
            $query[] = "UPDATE `table_name` SET `bind`=`id`";
            }
        }
        $query = implode(';',$query).';';
        $STH2 = $this->dbConn()->exec($query) ; 

PDO knows batch insert if statement is finished with ';'.
I made some modifications to the while loop to build an array and then implode the array to build the sql statement.
Still, the script executes in a few seconds, which is very good, but the script is not ending. Can you see any flaw in my code? The code is executing normally, database is updated, all 130000+ rows but the script gives me 500 internal server error after ~5 minutes.

UPDATE `table_name` SET `bind`=`id`

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