当查询失败时,我尝试使用mysqli_rollback()
。此嵌套子查询应返回 null,因为“qwerty”不存在。但是没有生成异常消息?
<?php
$con = mysqli_connect("x","y","z","zz");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
try {
mysqli_autocommit($con,FALSE);
mysqli_query($con,"INSERT INTO customer (id, name, mobile) values ((select id from somewhere where something = 'qwerty'), 'john', '01234567890')");
mysqli_commit($con);
} catch (Exception $e) {
echo 'Message: ' .$e->getMessage();
mysqli_rollback($con);
}
?>
创建下面给出的客户表:
CREATE table customer
(
id int NOT NULL,
name varchar(100) NOT NULL,
mobile varchar(100),
PRIMARY KEY (id)
);
最佳答案
查询不会导致错误,因此没有什么可捕获的。
$sql = 'select ....';
$result=$con->query($sql);
$count = $con->affected_rows;
if($count<0){do something because this is an error};
else if($count==0){do something when nothing inserted};
else {do something to tell me record was inserted};
关于php - 如果此查询失败,如何使用 mysqli_rollback,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27469739/