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.

1

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();
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.