1

I'm trying to set up a function for deleting a user's account using an all-or-nothing transaction. The main part of the function is as follows:

$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;
}

I've tried putting the queries into separate prepared statements, but either way I end up at this screen.

I'm using MAMP, and trying to interact with a MSSQL server. I'm using PHP 7.06 and mysqlnd is enabled.

Is this due to performing multiple queries at the same time, or would it be something to do with my MAMP-MSSQL connection?

Derek Y
  • 11
  • 4

1 Answers1

-3

This looks like a good opportunity to use a stored procedure. It would simplify your code greatly and make debugging much more simple too. Bonus: the transaction is probably faster too.

jce
  • 133
  • 1
  • 10