Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use transaction in php/mysql

Tags:

php

mysql

i am using php/mysql. i know about the transaction in mysql but can't use in my script. below is my script how can i use php transaction in my code.i.e BEGIN, ROLLBACK, COMMIT

foreach($json_a['shop'] as $jsondata=>$json)
{
if($json['category']==='product')
{
$name_product=$json['name'];
$query1="insert into product(id,name,user_id)values('','" . mysql_real_escape_string($name_product). "','1')";

$result1=mysql_query($query1) or die("error in query".mysql_errno());
//echo "success...!";
$product++;
}
else
if($json['category']==='order')
{
$name_order=$json['name'];
$query2="insert into order(id,name,user_id)values('','" . mysql_real_escape_string($name_order). "','1')";

$result2=mysql_query($query2) or die("error in query".mysql_errno());
$order++;
}
else
if($json['category']==='sale')
{
$name_sale=$json['name'];
$query3="insert into sale(id,name,user_id)values('','" . mysql_real_escape_string($name_sale). "','1')";

$result3=mysql_query($query3) or die("error in query".mysql_errno());
$sale++;
}
}
like image 977
hunter Avatar asked Mar 09 '11 06:03

hunter


3 Answers

Simply issue mysql_query('START TRANSACTION'); and check for errors at every one of your inserts. If one of them doesn't succeed issue a ROLLBACK immediately without doing any of the remaining queries. If everything goes fine with all of them issue a COMMIT.

It may be easier to put them in a try-catch block to avoid using too many levels of nesting with if-else.

// START TRANSACTION
try{
    // INSERT 1
    if(failed)
        throw new Exception();

    // INSERT 2
    if(failed)
        throw new Exception();

    // INSERT 3
    if(failed)
        throw new Exception();

    // COMMIT
}
catch(Exception $e){
    // ROLLBACK
}

You may also want to take a look into PHP's PDO extension. Transactions are part of its features.

like image 86
Alin Purcaru Avatar answered Oct 30 '22 12:10

Alin Purcaru


One option is to use PDO. Example:

$db = new PDO($dsn,$user,$password);

$db->beginTransaction();
$db->exec("delete from mytable");

$allGood = doSomethingElse();

if ($allGood)
{
  $db->commit();
} else {
  $db->rollBack();
}

or a more elegant method:

$db = new PDO($dsn,$user,$password);
$db->beginTransaction();

try{
  //first execution      
  $db->exec("delete from mytable");

  //second execution
  $db->exec("insert into anothertable");

  //if all went well
  $db->commit();

} catch (Exception $e) {

  //something broke, hit undo
  $db->rollBack();

}
like image 25
Christian Burger Avatar answered Oct 30 '22 12:10

Christian Burger


The same rules/syntax apply here as they do in regular MySQL statements regarding transactions.

Here's an example:

$query0 = 'START TRANSACTION;';
mysql_query($query0) or die('woops' . mysql_error());

/* all of your queries here */

$query5 = 'COMMIT;';
mysql_query($query5) or die('woops' . mysql_error());

More information on the MySQL syntax for transactions can be found here: http://dev.mysql.com/doc/refman/5.0/en/commit.html

like image 38
philwinkle Avatar answered Oct 30 '22 10:10

philwinkle