简体   繁体   English

PHP PDO无法删除记录

[英]PHP PDO can't delete record

I'm having a bit of a problem with PDO deleting a record from the database. PDO从数据库中删除记录时遇到了一些问题。

It just returns false, and I can't seem to see why, can anyone help? 它只是返回错误,我似乎无法理解为什么,任何人都可以帮忙吗? The code: 代码:

$db = new PDO('sqlsrv:Server='.DB_HOST.';Database='.DB_NAME, DB_USER, DB_PASS);
$query = $db->prepare('DELETE * FROM '.$table.' WHERE id = :id');
$query->bindValue(':id', $id);
$query->execute();

I've also tried: 我也尝试过:

$db = new PDO('sqlsrv:Server='.DB_HOST.';Database='.DB_NAME, DB_USER, DB_PASS);
$db->exec('DELETE * FROM '.$table.' WHERE id = '.$id);

And I know the user from the DB has permissions to delete because I can run the query with success in the SQL client. 我知道数据库中的用户有权删除,因为我可以在SQL客户端中成功运行查询。

Any ideas? 有任何想法吗?

Thanks :) 谢谢 :)

DELETE FROM $table WHERE id = $id
  • there is no * in the DELETE statement DELETE语句中没有*

Go here for some easy documentation . 到这里获取一些简单的文档

Your first example is fine, bar one error 你的第一个例子很好,一个错误

$db = new PDO('sqlsrv:Server='.DB_HOST.';Database='.DB_NAME, DB_USER, DB_PASS);
$query = $db->prepare('DELETE FROM '.$table.' WHERE id = :id');
$query->bindValue(':id', $id);
$query->execute();

You don't need a * in a simple delete statement. 在简单的删除语句中不需要*。

FYI, rowCount() will work well for confirming the delete worked, as follows: 仅供参考, rowCount()可以很好地确认删除工作,如下所示:

$countDel = $query->rowCount();
if ($countDel == 0) {
echo "No rows deleted";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM