php+;mysql仍在提交
我有3个从php执行的mysql查询,1个成功,2个不成功。但是,我已经设置了提交和回滚。当我在提交上执行var_uu转储时,它返回true,成功的查询会在db中插入值,而不是回滚 //连接已建立php+;mysql仍在提交,php,mysql,Php,Mysql,我有3个从php执行的mysql查询,1个成功,2个不成功。但是,我已经设置了提交和回滚。当我在提交上执行var_uu转储时,它返回true,成功的查询会在db中插入值,而不是回滚 //连接已建立 function __construct() { $this->error['State']=0; $this->error['Msg']=""; try { $db_con=mysqli_connect($this->server, $t
function __construct() {
$this->error['State']=0;
$this->error['Msg']="";
try {
$db_con=mysqli_connect($this->server, $this->dbuname, $this->dbpassw, $this->database);
$this->db_connection=$db_con;
mysqli_autocommit($db_con,FALSE);
mysqli_begin_transaction($this->db_connection);
}
catch (Exception $e) {
$this->setError("Error: Please try again");
}
}
//查询和提交
public function result($sql) {
try {
$result=mysqli_query($this->db_connection,$sql);
if($result) {
$this->result=$result;
}else {
$this->setError("Error: Please try again");
}
}
catch (Exception $e) {
$this->setError("Error: Please try again");
}
}//end of result function
public function commit() {
$mysqli=$this->db_connection;
$c=mysqli_commit($mysqli);
if (!$c) {
$this->setError("Error: Please try again");
mysqli_rollback($mysqli);
}else{
return 1;
}
}
//质疑
public function make($i){
$sql="INSERT INTO t1(tcol1, tcol2, tcol3) VALUES ";
...
$this->commit();
$this->close_conn();
如果您告诉它提交,它将提交,谢谢。它不会因为其中一条语句中出现错误而回滚。每次执行查询时都必须检查错误,如果有错误,必须回滚。或者在提交之前在末尾检查:
public function commit() {
$mysqli=$this->db_connection;
if ($this->error['State'] === 0) {
$this->setError("Error: Please try again");
mysqli_rollback($mysqli);
} else {
$c = mysqli_commit($mysqli);
if ($c) return 1;
}
}
问题是表是isam,数据库是innoDB。当我将表更改为innoDB时,它开始工作。好的,谢谢,认为提交有某种错误检查机制。提交的实际好处不是只有在正确的情况下才执行所涉及的查询吗?@bestprogrammerintheworld好处是我可以确保执行所有或任何有效查询。如果我在做一个长时间的手工事务,我可能会花一个小时编写不同的查询,并意外地拼写“INSERT”错误。我会收到一条错误消息。我不想从头开始,我只是修正了拼写错误并继续工作。如果我意识到我这样做的方式行不通,我可以后退。如果我看到一切看起来都很好,我可以承诺。在很多用例中,您可能会遇到错误并希望处理错误,而不是rollback@dave-好吧,啊哈,现在我明白你的意思了。谢谢!:-)我鼓励你读你写的东西。你会发现你从来没有问过问题,只是提出了一个情况