Mysqli to execute MySQL transaction

Source: Internet
Author: User
Tags foreach commit manual rollback

public static function Exec_sql_tran ($arr _sql) {

$result =true;

$con = new mysqli (dbhost,db_user,db_passwd,db_name);

$con->autocommit (FALSE);

foreach ($arr _sql as $key => $sql) {

if (! $con->query ($sql)) {

echo $con->error. "

/";

$resulf =false;

$con->rollback ();

Break

}

}

Print_r ($result);

if ($result) {

$con->commit ();

}

else{

$con->rollback ();

}

$con->close ();

}

$arr _sql is an array of stored database code

When I tested it, I wrote two insert sentences, the first sentence was correct, the second sentence was wrong, and the execution of the first sentence was always found to be successful.

As if the business is useless, PHP novice, for advice

Only InnoDB and BDB types of data tables in MySQL can support transaction processing

I execute show engines in MySQL, see InnoDB, the result shows that it is support transaction

# Engine, Support, Comment, transactions, XA, savepoints

' InnoDB ', ' DEFAULT ', ' Supports transactions, Row-level locking, and foreign keys ', ' yes ', ' yes ', ' yes '

$result =true;

$resulf =false;

Do you see the problem? The following variable name is incorrectly written

Found, the Sins of sin ~

public static function Exec_sql_tran ($arr _sql) {

$result =true;

$con = new mysqli (dbhost,db_user,db_passwd,db_name);

$con->autocommit (FALSE);

foreach ($arr _sql as $key => $sql) {

if (! $con->query ($sql)) {

echo $con->error. "

";

$result =false;

Break

}

}

if ($result) {

$con->commit ();

}

else{

$con->rollback ();

}

$con->close ();

}

Or the problem, after I changed the code, such as two sql,sql1 correct, sql2 error, using this method to perform, SQL1 data is correctly inserted into the database

But if sql1 wrong, sql2 correct words, two can not insert success, this is actually the role of break, but the role of the business does not reflect AH

public static function Exec_sql_tran ($arr _sql) {

$result =true;

$con = new mysqli (dbhost,db_user,db_passwd,db_name);

$con->autocommit (FALSE);

foreach ($arr _sql as $key => $sql) {

if (! $con->query ($sql)) {

echo $con->error. "

";

$result =false;

Break

}

}

if ($result) {

$con->commit ();

}

else{

$con->rollback ();

}

$con->close ();

}

Or the problem, after I changed the code, such as two sql,sql1 correct, sql2 error, using this method to perform, SQL1 data is correctly inserted into the database

But if sql1 wrong, sql2 correct words, two can not insert success, this is actually the role of break, but the role of the business does not reflect AH

A statement that does not see the start of a transaction.

$con->begin_transaction ();

public static function Exec_sql_tran ($arr _sql) {

$result =true;

$con = new mysqli (dbhost,db_user,db_passwd,db_name);

$con->autocommit (FALSE);

foreach ($arr _sql as $key => $sql) {

if (! $con->query ($sql)) {

echo $con->error. "

";

$result =false;

Break

}

}

if ($result) {

$con->commit ();

}

else{

$con->rollback ();

}

$con->close ();

}

Or the problem, after I changed the code, such as two sql,sql1 correct, sql2 error, using this method to perform, SQL1 data is correctly inserted into the database

But if sql1 wrong, sql2 correct words, two can not insert success, this is actually the role of break, but the role of the business does not reflect AH

A statement that does not see the start of a transaction.

$con->begin_transaction ();

Mysqli inside I did not find begin_transaction () This method ...

http://www.php.net/manual/zh/mysqli.begin-transaction.php

What is this?

I see this.

(PHP 5 >= 5.5.0)

Mysqli::begin_transaction–mysqli_begin_transaction-starts a transaction

is PHP version more than 5.5.0 for transactions?

http://www.php.net/manual/zh/mysqli.begin-transaction.php

What is this?

I call this method inside the code to show the undefined

Look at the engine of the watch, I doubt it's innodb.

Look at the engine of the watch, I doubt it's innodb.

I'm sure the database engine is InnoDB, how does the table look?

Look at the engine of the watch, I doubt it's innodb.

It's the engine of the watch that hasn't changed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.