Mysqli execution of mysql transactions

Source: Internet
Author: User

Mysqli execution of mysql transactions

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 that stores database code.

I wrote two insert sentences during the test. The first sentence is correct and the second sentence is wrong. At that time, I found that the first sentence was successfully executed.

It seems that the transaction is useless. I'm new to php and want to learn more.

In MYSQL, only INNODB and BDB Data Tables support transaction processing.

I run show engines in mysql and check InnoDB. The result shows that transactions are supported.

# Engine, Support, Comment, Transactions, XA, Savepoints

'Innodb', 'default', 'ororts transactions, row-level locking, and foreign keys ', 'yes'

$ Result = true;

$ Resulf = false;

Can you see the problem? The following variable name is incorrect.

Yes, it's a 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 ();

}

Still, after I changed the code, for example, if two SQL statements, sql1 is correct and sql2 is incorrect, execute this method and the sql1 data is correctly inserted into the database.

However, if sql1 is incorrect and sql2 is correct, the two cannot be successfully inserted. This is actually the role of break, but the role of the transaction is not reflected.

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 ();

}

Still, after I changed the code, for example, if two SQL statements, sql1 is correct and sql2 is incorrect, execute this method and the sql1 data is correctly inserted into the database.

However, if sql1 is incorrect and sql2 is correct, the two cannot be successfully inserted. This is actually the role of break, but the role of the transaction is not reflected.

The statement starting the transaction is not displayed.

$ 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 ();

}

Still, after I changed the code, for example, if two SQL statements, sql1 is correct and sql2 is incorrect, execute this method and the sql1 data is correctly inserted into the database.

However, if sql1 is incorrect and sql2 is correct, the two cannot be successfully inserted. This is actually the role of break, but the role of the transaction is not reflected.

The statement starting the transaction is not displayed.

$ Con-> begin_transaction ();

In mysqli, The begin_transaction () method is not found...

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 there a transaction in php versions later than 5.5.0?

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

What is this?

I call this method in the code to display undefined

Let's look at the table engine. I suspect it's not INNODB.

Let's look at the table engine. I suspect it's not INNODB.

I'm sure the database engine is INNODB. What about tables ~?

Let's look at the table engine. I suspect it's not INNODB.

The problem is that the table engine is not 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.