behz4d
behz4d

Reputation: 1849

Checking status of a Transaction - MySQL and PHP

I want to use Transactions in MySQL, my questions is that How I can check if the queries are submitted successfully or not? my query is:

mysql_query("START TRANSACTION");
$query_1 = mysql_query("DELETE FROM BLAH WHERE BLAH");
$query_2 = mysql_query("DELETE FROM BLAH WHERE BLAH");
mysql_query("COMMIT");

as far as I know, the above code does the transaction, but I want to check if the queries are submitted successfully or not, so:

    mysql_query("START TRANSACTION");
    $query_1 = mysql_query("DELETE FROM BLAH WHERE BLAH");
    $query_2 = mysql_query("DELETE FROM BLAH WHERE BLAH");
    if($query_1 && $query_2){
       mysql_query(COMMIT);
    }else{
       mysql_query(ROLLBACK);
    }

But I read somewhere that in transactions, the queries submit when we call "COMMIT", so the above code should not work since while we check $query_1 && $query_2, actually nothing is submitted to db since it's before the "COMMIT" query, how I could perform such check?

Thanks in advance

P.S: Am I doing the whole thing right? please kindly let me know if there are also some other better ways... thanks

Upvotes: 2

Views: 2578

Answers (1)

Lix
Lix

Reputation: 47956

If you are performing DELETE commands, you'll be able to see if there was any affected rows by calling the mysql_affected_rows() function.

mysql_affected_rows — Get number of affected rows in previous MySQL operation

There is even mention of use in conjunction with transactions in the documentation -

Note: Transactions If you are using transactions, you need to call mysql_affected_rows() after your INSERT, UPDATE, or DELETE query, not after the COMMIT.


It should be noted here that you are using an old version of database connections. See the php documentation on any page related to mysql_* commands and you'll encounter this message -

This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.

Upvotes: 1

Related Questions