MySQLi准备的语句和事务

MySQLi准备的语句和事务

本文介绍了MySQLi准备的语句和事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以处理准备好的报表?

Is there a way to do transactions with prepared statements?

我的意思是我可以对$mysqli->autocommit(FALSE);$mysqli->commit( );$mysqli->rollback( );

I mean can I use the following example with $mysqli->autocommit(FALSE); and $mysqli->commit( ); and $mysqli->rollback( );

//Preparing the statment

$insert_stmt=$mysqli->prepare("INSERT INTO x VALUES(?,?)")
or die($mysqli->error);

//associate variables with the input parameters

$insert_stmt->bind_param("is", $my_number,$my_string); //i=integer

//Execute the statement multiple times....

for ($my_number = 1; $my_number <= 10; $my_number++)
{
    $my_string="row ".$my_number;
    $insert_stmt->execute() or die ($insert_stmt->error);
}
$insert_stmt->close();

我环顾四周,但找不到使用准备好的语句(不是PDO)的任何示例: http://php.net/manual/en/mysqli.prepare.php 对于事务,我只能找到此处提到的示例: http://book.opensourceproject.org.cn/lamp/mysql/mysqlstored/opensource/0596100892/mysqlspp-chp-13-sect-2.html 其中的事务和准备好的语句是永远不要混在一起.

I have looked around but can't find any example of using prepared statements (not PDO):http://php.net/manual/en/mysqli.prepare.php with transactions, I only find examples like these mentioned here: http://book.opensourceproject.org.cn/lamp/mysql/mysqlstored/opensource/0596100892/mysqlspp-chp-13-sect-2.html where transactions and prepared statements are never inter-mixed.

一起使用它们会错吗?

推荐答案

准备的语句和事务是无关的技术.

Prepared statements and transactions are unrelated techniques and technologies.

您可能希望直接发出START TRANSACTIONCOMMIT/ROLLBACK命令,而不是使用专用方法.它们在功能上是等效的.

You may wish to issue the START TRANSACTION and COMMIT/ROLLBACK commands directly instead of using the dedicated methods. They are functionally equivalent.

对于循环,您可以在prepare之前发出START TRANSACTION ,然后在循环退出后发出COMMIT.在准备好的语句开始之后但在执行之前,您可能不应该尝试打开事务.

For your loop, you'd issue the START TRANSACTION before your prepare, then your COMMIT after the loop exits. You probably should not try to open a transaction after a prepared statement has been started but before it's been executed.

由于某种原因,他们没有添加开始事务"命令来关闭自动提交功能.这是有关mysqli的怪异事物之一,这使我始终推荐使用PDO. :)隐式打开事务将在事务期间关闭自动提交.

For some reason, they didn't add a "start transaction" command in favor of turning off autocommit. It's one of those weird things about mysqli that makes me always recommend PDO instead. :) Opening a transaction implicitly turns off autocommit for the duration of the transaction.

这篇关于MySQLi准备的语句和事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 01:57