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

标签 php mysql pdo

我决定尝试一下 PDO。下面的函数应该将表中的一行移动到另一个数据库(从登台到生产),然后删除登台中的行。它是 2 个完全独立的数据库。

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

也请随时给我更好的最佳实践。我认为我的代码不够优雅,并认为有更好的方法可以做到这一点,尤其是异常(exception)情况

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

最佳答案

我假设这是一个转义问题。

试试这个:

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

关于php - PDO 无法将行移动到不同的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11135934/

相关文章:

PHP PDO 更新查询不起作用

php - 在 Laravel 中使用时区将默认数据库 DateFormat 设置为 ISO 8601

php - mySql 获取数组忽略空值

php比较日期

php - 在多个域中包含单个 javascript 文件

php - ffmpeg - 来自 rtsp 的实时视频,带有 mp4 碎片

php - 找出您的 PHP 代码在哪里变慢(性能问题)

php - MYSQL关注者系统: Indexing the Followers Table

php - 重复 key 更新后插入进程

javascript - 使用 Ajax 登录 PHP