我的查询包含' INSERT',' DELETE'并且'更新'声明。现在我的问题是"如果任何其他声明失败,是否可能没有语句运行"
$sql=" START Transaction INSERT statement; DELETE statement; UPDATE statement; COMMIT";
答案 0 :(得分:0)
您必须使用MySQL transactions。如果出现问题(例如,您收到错误),它将允许您回滚所有更改(在事务中进行的更改)。
简单示例:
<?php
$all_query_ok=true; // our control variable
$mysqli = new mysqli("localhost", "user", "pass", "dbname");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* disable autocommit */
$mysqli->autocommit(FALSE);
//we make 4 inserts, the last one generates an error
//if at least one query returns an error we change our control variable
$mysqli->query("INSERT INTO myCity (id) VALUES (100)") ? null : $all_query_ok=false;
$mysqli->query("INSERT INTO myCity (id) VALUES (200)") ? null : $all_query_ok=false;
$mysqli->query("INSERT INTO myCity (id) VALUES (300)") ? null : $all_query_ok=false;
$mysqli->query("INSERT INTO myCity (id) VALUES (100)") ? null : $all_query_ok=false; //duplicated PRIMARY KEY VALUE
//now let's test our control variable
$all_query_ok ? $mysqli->commit() : $mysqli->rollback();
$mysqli->close();
?>