直接关闭MysqLi页面:http://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();
}
/* set autocommit to off */
$MysqLi->autocommit(FALSE);
/* Insert some values */
$MysqLi->query("INSERT INTO table1 VALUES ('DEU','Bavarian','F',11.2)");
$MysqLi->query("INSERT INTO table2 VALUES ('DEU',11.2)");
/* commit transaction */
$MysqLi->commit();
/* close connection */
$MysqLi->close();
*编辑准备好的“非理智”行动声明:
PHP
$MysqLi = new MysqLi("localhost","root","","");
/* check connection */
if (MysqLi_connect_errno()) {
printf("Connect Failed: %s\n",MysqLi_connect_error());
exit();
}
/* set autocommit to off */
$MysqLi->autocommit(FALSE);
$stmt1 = $MysqLi->prepare("INSERT INTO tbl1 (id,intro) VALUES (?,?)");
$stmt2 = $MysqLi->prepare("INSERT INTO tbl2 (id,name) VALUES (?,?)");
$str1 = 'abc';
$str2 = 'efg';
$str3 = 'hij';
$str4 = 'klm';
$stmt1->bind_param('ss',$str1,$str2);
$stmt2->bind_param('ss',$str3,$str4);
if ($stmt1->execute() == false)
{
echo 'First query Failed: ' . $MysqLi->error;
}
$stmt1->close();
if ($stmt2->execute() == false)
{
echo 'Second query Failed: ' . $MysqLi->error;
}
$stmt2->close();
$MysqLi->close();