我已经学会了使用pdo进行提交和回滚。当我使用简单的query()
进行操作时,效果很好,但是当我为exec()
添加事务时,rollback()
不起作用。
$tables = array("applicant_course", "applicant_course_time", "course",'session_course_sort_data','sessions_attendee','sessions');
$conn = new PDO("mysql:host=" . $db_host_target . ";port=".$db_port_target.";dbname=" . $db_name_target , $db_user_target, $db_password_target);
$conn->setAttribute(PDO::ATTR_AUTOCOMMIT,0);
$conn->beginTransaction();
try {
foreach ($tables as $value) {
$sql_file = fopen($value . '.sql', 'r');
$sql = fread($sql_file, filesize($value . '.sql'));
$conn->exec($sql); //here is exec()
}
$conn->rollback();//I have put rollback here for testing purposes
} catch (Exception $e) {
// An exception has been thrown
// We must rollback the transaction
echo 'error<br>';
$conn->rollback();
}
您能指导我为exec()
运行事务该怎么做。如果您需要,我也可以上传.sql
文件。谢谢