0

Hi guys I want to commit the transaction if all the queries executed in the loop, or else I need to rollback how do i do this, I tried this , but I am not sure how efficient it is.

       mysql_query("START TRANSACTION");

for($i=0;$i<100;$i++)
{
    $res=mysql_query("some query")
    array_push($array,$res);
}


if(in_array(0,$array))
          mysql_query("rollback");
           else
          mysql_query("commit");

If its there any efficient method plese let me know, any help will be much appriciated,
Thanks in advance.

Edited by Nick Evan: Fixed formatting

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by mschroeder
0

While I'm sure this can be done with the very old mysql extension, mysqli makes this very easy. This is taken from the php mysqli::commit documentation. http://www.php.net/manual/en/mysqli.commit.php

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$mysqli->query("CREATE TABLE Language LIKE CountryLanguage");

/* set autocommit to off */
$mysqli->autocommit(FALSE);

/* Insert some values */
$mysqli->query("INSERT INTO Language VALUES ('DEU', 'Bavarian', 'F', 11.2)");
$mysqli->query("INSERT INTO Language VALUES ('DEU', 'Swabian', 'F', 9.4)");

/* commit transaction */
$mysqli->commit();

/* drop table */
$mysqli->query("DROP TABLE Language");

/* close connection */
$mysqli->close();
?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.