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.