下面是我的问题
$db= new mysqli('localhost','user','passcode','dbname');
try {
// First of all, let's begin a transaction
$db->beginTransaction();
// A set of queries; if one fails, an exception should be thrown
$query1="insert into table1(id,name) values('1','Dan')";
$query2="insert into table2(id,name) values('2','Anaba')";
// If we arrive here, it means that no exception was thrown
// i.e. no query has failed, and we can commit the transaction
$db->commit();
} catch (Exception $e) {
// An exception has been thrown
// We must rollback the transaction
$db->rollback();
}
我希望通过PHP在MySQL查询中实现事务,当其中一个查询失败时,我想要所有的查询都失败。上面的代码会抛出一个错误("Fatal error: Call to undefined method mysqli::beginTransaction()")。请问是否有更好的解决方案或想法来解决这个问题。感谢您的帮助。