简体   繁体   中英

Transactions MySql and php

I'm confused abt running transactions through php script. In my MySql autocommit is set to 1, yet when I run a script

ex: mysql_query("START TRANSACTION");

$sql="INSERT INTO 
`address`(`Address_Id`, 
`Address_Line1`, 
`Address_line2`, 
`Zip`, 
`Created_By`,
 `Created_Date`, 
 `Updated_By`, 
 `Updated_Date`) 
VALUES ('2','this is test1','test2','210','SYSTEM','NOW()','SYSTEM','NOW()')";

mysql_query($sql);

When I don't give commit, inserted records in the table is zero. the record comes up only if I give commit command.

Also please tell me what happens if in a php script I start transaction and then dont give either roll back or commit statements

http://dev.mysql.com/doc/refman/5.5/en/commit.html :

By default, MySQL runs with autocommit mode enabled. This means that as soon as you execute a statement that updates (modifies) a table, MySQL stores the update on disk to make it permanent.

But if you specify START TRANSACTION explicitly - then it isn't taken into account, so you work in a transaction that should be committed.

Try to do like this.

mysql_query("BEGIN");

        $query="";
        $result=mysql_query($query);

        if($result==0)
        {
            mysql_query("ROLLBACK");
           echo $error_message;
          // mysql_query($query_setId);
           die();
        }

    mysql_query("COMMIT");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM