我在查询失败时尝试使用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)
);
答案 0 :(得分:1)
查询不会导致错误,因此无需捕捉。
$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};