PDO无法将行移动到不同的数据库

时间:2012-06-21 10:09:02

标签: php mysql pdo

我决定去PDO。下面的函数应该将表中的一行移动到另一个数据库(从分段到生产),然后删除分段中的行。它是2个完全分离的数据库。

我能够获取行并将其插入到生产数据库中,但是它不会删除登台数据库中的行并且给我一个错误,说明错误的字段名称343这是行的ID,我不是确定为什么它认为它是一个字段名,它实际上是值。

请随时给我更好的最佳做法。我没有看到我的代码优雅,并假设有更好的方法来做到这一点,尤其是例外

 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());
    }
}

1 个答案:

答案 0 :(得分:1)

我认为这是一个逃避问题。

试试这个:

...
try {
    $sql = "DELETE FROM `calls` WHERE `id`= :id";
    $stmtx = $this->staging->prepare($sql);
    $stmtx->execute(array($this->call['info']['call_id']));
    if(!query) {
...