我正在使用PDO运行多个查询.如果第二个查询失败,则抛出异常.
$db = new PDO("MysqL:host=localhost;dbname=test",'root',''); $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); $sql = " DELETE FROM car; INSERT INTO car(name,type) SELECT name,from FROM vehicle; "; try { $db->exec($sql); } catch (PDOException $e) { echo $e->getMessage(); die(); }
上面的代码执行时没有抛出异常.
我在使用准备好的声明时找到了答案.循环遍历所有行集后,我可以使用$stmt-> errorInfo()检查上次执行的查询是否导致错误.
原文链接:/php/137276.html$db = new PDO("MysqL:host=localhost;dbname=test",PDO::ERRMODE_SILENT); $db->setAttribute(PDO::ATTR_EMULATE_PREPARES,1); $sql = " DELETE FROM car; INSERT INTO car(name,from FROM vehicle; "; $stmt = $db->prepare($sql); $stmt->execute(); $i = 0; do { $i++; } while ($stmt->nextRowset()); $error = $stmt->errorInfo(); if ($error[0] != "00000") { echo "Query $i Failed: " . $error[2]; die(); }