PHP-如何验证记录已成功删除

时间:2018-12-02 07:39:55

标签: php mysql delete-row

这是我用于从数据库中删除行的代码:

<?php
$eid = $_GET['eid'];
$con = mysqli_connect("localhost", "root", "","project") or die("Connection failed");
echo "connection is done";
$query = "delete from exam where eid='$eid'";

if ($con->query($query)==TRUE)
{
    echo " record deleted";
}
else 
{
    echo "Error: " . $query . "<br>" . $con->error;
}

$con->close();
?>

else语句未得到执行。即使在数据库中找不到该值,它也会为每个值显示“已删除记录”。

为什么会这样?如何验证我的记录已从数据库中删除?

1 个答案:

答案 0 :(得分:4)

您可以使用mysqli.affected-rows

请考虑以下内容:

$query="delete from exam where eid='$eid'";
if ($con->query($query)==TRUE && $con->affected_rows > 0) {
    echo " record deleted";
} else {
    echo "Error: " . $query . "<br>" . $con->error;
}