Reputation: 3592
I decided to have a go at PDO. The function below should move a row in a table to another database (from staging to production) and then delete the row in staging. It is 2 completely seperated databases.
I am able to get the row and insert it into the production database, but it does not delete the row in the staging database and gives me an error saying incorrect field name 343 which is the id of the row, I am not sure why it thinks it's a fieldname where it is infact the value.
Please also feel free to give me better best practises. I don't see my code as elegant and assume that there are better ways to do this, esspecially with the exceptions
private function moveCallToProduction() {
try {
$array = array(":id" => $this->call['info']['call_id']);
$sql = "SELECT * FROM `calls` WHERE `id`=:id";
$query = $this->staging->prepare($sql);
$query->execute($array);
$row = $query->fetch(PDO::FETCH_NUM);
try {
$sql = "INSERT INTO `calls` (`id`,`sip_id`,`extension`,`caller_id`,`stage`,`status`,`survey_id`,`start`,`answer`,`hangup`) VALUES (`?`,`?`,`?`,`?`,`?`,`?`,`?`,`?`,`?`,`?`)";
$stmt = $this->production->prepare($sql);
$stmt->execute($row);
if(!$stmt) {
throw new Exception('Unable to move the call '.$this->call['info']['call_id'].' to the production server.');
} else {
try {
$sql = "DELETE FROM `calls` WHERE `id`='".$this->call['info']['call_id']."'";
$query = $this->staging->query($sql);
if(!$query) {
throw new Exception('Unable to delete call '.$this->call['info']['call_id'].' from the staging server.');
}
}
catch(PDOException $e) {
$this->informer("FATAL",$e->getMessage());
}
}
}
catch(Exception $e) {
$this->informer("FATAL",$e->getMessage());
}
}
catch(PDOException $e) {
$this->informer("FATAL","We're unable to transport the call from the staging to production server. Error: ".$e->getMessage());
}
}
Upvotes: 1
Views: 103
Reputation:
I am assuming that it is an escaping problem.
Try This:
...
try {
$sql = "DELETE FROM `calls` WHERE `id`= :id";
$stmtx = $this->staging->prepare($sql);
$stmtx->execute(array($this->call['info']['call_id']));
if(!query) {
...
Upvotes: 1