PHP PDO多个删除查询不起作用

时间:2017-02-23 00:20:01

标签: php sql-server pdo sql-delete multiple-tables

我尝试使用全有或全无的交易设置删除用户帐户的功能。该功能的主要部分如下:

$success = true;
$this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1);
$this->db->beginTransaction();

try
{

    if( $data = $this->get_ids($account_id) ){
        $sql = "DELETE FROM table1 WHERE [ID] IN (:loc_ids);
            DELETE FROM table2 WHERE [ID] IN (:spid);
        ";
        $success = $success && $this->db->prepare($sql)->execute(array(':loc_ids' => $data['loc_ids'], ':spid' => $data['spid']));
    }

    $sql = "DELETE FROM table3 WHERE [ACCOUNT_ID] = :account_id;
        DELETE FROM table4 WHERE ([ACCOUNT_ID] = :account_id OR [USER_ID] = :user_id);
        DELETE FROM table5 WHERE [ACCOUNT_ID] = :account_id;
        DELETE FROM table6 WHERE [ACCOUNT_ID] = :account_id;
        DELETE FROM table7 WHERE [ACCOUNT_ID] = :account_id;
    ";
    $success = $success && $this->db->prepare($sql)->execute( array(':account_id' => $account_id, ':user_id' => $uid) );

    if( !$success ){
        throw new Exception("Error Processing Request", 1);
    }

    $this->db->commit();
    return true;

}
catch (PDOException $exc)
{
    echo $exc->getMessage();
    $this->db->rollBack();
    return false;
}

我已经尝试将查询放入单独的预准备语句中,但无论哪种方式,我都会以this screen结束。

我正在使用MAMP,并尝试与MSSQL服务器进行交互。我使用PHP 7.06并启用了mysqlnd。

这是由于同时执行多个查询,还是与我的MAMP-MSSQL连接有关?

1 个答案:

答案 0 :(得分:-3)

这看起来是使用存储过程的好机会。它可以大大简化您的代码并使调试更加简单。奖金:交易也可能更快。