我写了这个,但事务不起作用,而且我还将两个表转换为 innodb 类型,任何人都可以指导我编码或另一种事务替代方案中的问题。
mysql_query("begin;");
$query1 = mysql_query("ALTER TABLE products ADD COLUMN {$_POST[fields]} VARCHAR(60)");
$query2 = mysql_query("INSERT INTO fields (cid5,fields,field_title,field_type)
VALUE ('$_POST[cid]','$_POST[fields]','$_POST[field_title]','$_POST[field_type]')");
if (($query1)&&($query2)) {mysql_query("commit;");}
else {mysql_query("rollback;");}
}
我正在使用 mysql 5.1.69-cll
最佳答案
ALTER TABLE是一条DDL(数据定义语言)语句;这在 MySQL innodb 引擎中不是事务性的。 INSERT 是一个 DML 语句(数据操作语言),它是事务性的。由于一条语句不是事务性的,而另一条语句是事务性的,因此不应将两者合并在一个事务中。
引用MySQL手册:
Some statements cannot be rolled back. In general, these include data definition language (DDL) statements, such as those that create or drop databases, those that create, drop, or alter tables or stored routines.
You should design your transactions not to include such statements. If you issue a statement early in a transaction that cannot be rolled back, and then another statement later fails, the full effect of the transaction cannot be rolled back in such cases by issuing a ROLLBACK statement.
http://dev.mysql.com/doc/refman/5.6/en/cannot-roll-back.html
关于php - mysql 事务不适用于 innodb 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18504981/