如何使用PHP验证MySQL中的DELETE语句是否成功?查看带注释的区域。
if ($stmt = $db->prepare($query))
{
$stmt->bind_param('i', $useid);
$stmt->execute();
$result = $stmt->get_result();
// did DELETE call succeed and not error out?
$stmt->close();
}
答案 0 :(得分:3)
结果必须返回true,然后使用$stmt->affected_rows
if ($stmt = $db->prepare($query))
{
$stmt->bind_param('i', $useid);
$result_del=$stmt->execute();
if($result_del){
printf("rows deleted: %d\n", $stmt->affected_rows);
}
// did DELETE call succeed and not error out?
$stmt->close();
}
答案 1 :(得分:0)
if ($stmt = $db->prepare($query))
{
$stmt->bind_param('i', $useid);
$result = $stmt->execute();
if ($result) {
echo "call succeeded"; # means no syntax error
}
if ($stmt->affected_rows > 0) {
echo "at least one row was deleted";
}
$stmt->close();
}
答案 2 :(得分:0)
您可以检查execute()函数的返回值
if ($stmt = $db->prepare($query))
{
$stmt->bind_param('i', $useid);
if($stmt->execute()) {
//Action to perform if successful
}
else {
//Action to perform on failure
}
$stmt->close();
}
答案 3 :(得分:0)
像这样:
if ($stmt = $db->prepare($query))
{
$stmt->bind_param('i', $useid);
$stmt->execute();
$result = $stmt->get_result();
$deleted = $stmt->affected_rows;
print("Deleted $deleted rows.\n");
$stmt->close();
}
affected_rows()
可让您知道最近一次查询会影响多少行。