在我的桌子上
id |name
1 | one然后
$id = '2';
$sql=$this-con->query("delete from aTable WHERE id = '".$id."' " );行将执行以下操作
/* Affected rows: 0 Found rows: 0 Warnings: 0 Duration for 1 query: 0.000 sec. */即使表中没有这样的id 2。
我怎么能用PHP或者SQL来阻止这种情况呢?因为在我的PHP中,我需要告诉用户没有这样的行,但是如何在不首先检查自定义code.Because的情况下捕获丢失的行:
if($sql){
// Tell user the row is deleted, but (in fact there is no such row with the id in the column)
}
else{
// there must be exception goes here
}你明白我的意思吗?或者没有这样的方式,除非先进行自定义检查?
发布于 2015-06-15 16:12:05
PDO::exec()返回由您发出的SQL语句修改或删除的行数。如果没有任何行受到影响,PDO::exec()将返回0。
试试这个:
$nrows =$this-con->查询(“从aTable中删除id =‘’.$id.‘”)->fetchColumn();
发布于 2015-06-15 16:30:06
您是在PHP中使用PDO还是纯MySQL函数?
对于PDO,您可以使用准备语句,然后获得如下所示的rowCount:
$st = $conn->prepare("delete from aTable WHERE id = '".$id."' " );
$st->execute();
if($st->rowCount()) {
// return the number of affected rows: Record deleted
} else {
// no such record in the database
}如果您使用的是纯MySQL函数(实际上不应该使用它们),那么可以使用mysql_affected_rows():
$result = mysql_query("delete from aTable WHERE id = '".$id."' ");
if(mysql_affected_rows()) {
// record found and deleted
} else {
// no such record in the database
}https://stackoverflow.com/questions/30849738
复制相似问题