Php 在MYSQLi中报告未知索引时出错
我试图调用一个函数来更新我的mysqli数据库,并在发生错误时报告。到目前为止,我的测试文件中有:Php 在MYSQLi中报告未知索引时出错,php,mysqli,Php,Mysqli,我试图调用一个函数来更新我的mysqli数据库,并在发生错误时报告。到目前为止,我的测试文件中有: <?php $success = test("cat",17); echo $success; echo "<br />"; function test($name, $number){ $db = new mysqli("localhost","root","","test"); if($db->connect_errno){ ret
<?php
$success = test("cat",17);
echo $success;
echo "<br />";
function test($name, $number){
$db = new mysqli("localhost","root","","test");
if($db->connect_errno){
return "Failed to connect to MYSQL Database: [" . $db->connect_errorno . "]" . $db->connect_error;
}
if(!($stmt = $db->prepare("UPDATE `test` SET `name` = ? WHERE `number` = ?"))){
return "prepare failed: [" . $db->errno . "]" . $db->error;
}
if(!($stmt->bind_param('si', $name, $number))){
return "bind failed: [" . $db->errno . "]" . $db->error;
}
if(!($stmt->execute())){
return "execute failed: [" . $db->errno . "]" . $db->error;
}
$stmt->close();
return "Successfully updated database!";
}
?>
在我的测试中(cat,17),它返回一个成功,即使没有数字为17的列,而且我在数据库中没有看到任何变化。如果在WHERE子句中找不到数字,如何使其正确返回故障?在execute()
完成后,检查$stmt->受影响的行是否大于零:
if(!($stmt->execute())){
return "execute failed: [" . $db->errno . "]" . $db->error;
}
if(!$stmt->affected_rows) {
return "no rows updated";
}
不更改任何记录或不返回任何行的查询不是“失败”。它只是一个空的结果集。