So I am trying to “move” selected rows from 1 table to another in different databases.
It in theory works (but if anyone wants to give any opinions please do, I am very new to PDO. I however keep getting a “SQLSTATE[HY000]: General error” error.
Any advice?
private function broken() {
try {
$sql = "SELECT * FROM `calls` WHERE `calls`.`status`=0 AND `calls`.`stage` < 4 AND `calls`.`answer` < (NOW() + INTERVAL 10 MINUTE)";
$query = $this->staging->query($sql);
while($row = $query->fetch(PDO::FETCH_ASSOC)) {
// Insert in production database:
$sql = "INSERT INTO `ivr_incomplete` (`id`,`sip_id`,`extension`,`caller_id`,`stage`,`status`,`survey_id`,`start`,`answer`,`hangup`,`end`) VALUES (:id, :sip_id, :extension, :caller_id, :stage, :status, :survey_id, :start, :answer, :hangup, :end)";
$query = $this->production->prepare($sql);
$query->execute($row);
// Delete from staging:
$sql = "DELETE FROM `calls` WHERE `id`='".$row['id']."'";
$this->staging->query($sql);
}
}
catch(PDOException $e) {
$this->informer("FATAL", "Unable to process broken IVR surveys. Error: ".$e->getMessage());
}
}
Two points:
You are preparing the
INSERTon every iteration, which sort of eliminates half of the point of using a prepared statement – all you are using it for is escaping. One of the points of prepared statements is that the query is only parsed once, so if you need to execute the same query repeatedly with different values, callingprepare()once and then simply callingexecute()with the different data sets can significantly boost performance.This whole thing could be accomplished in 2 queries:Removed due to use of two separate DB connectionsEDIT
Try this code:
You will likely need to adjust the error handling to meet your needs, particularly around how it is handled if there is an error with an
INSERT, since I doubt you would want to break the whole operation and leave the rows that have been successfully processed in the source table.