2017-02-23 72 views
0

我試圖設置一個功能來刪除一個用戶的帳戶使用全有或無的交易。該函數的主要部分如下:PHP PDO多個刪除查詢不起作用

$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連接有關?

回答

-3

這看起來像是使用存儲過程的好機會。它將大大簡化您的代碼,並使調試更加簡單。獎勵:交易也可能更快。