PHP/PDO:使用简单的准备好的语句与查询返回/受影响的行?
我对 PDO 对象很陌生,找不到任何可以帮助我的文档。假设我有一个简单的代码来删除一行:
$count = $dbh->exec("DELETE FROM fruit WHERE colour = 'red'");
这将返回受影响的行,但是我将如何使用准备好的语句呢?可以使用 $dbh->prepare
AND $dbh->exec
或查询!?
I am new to PDO objects and cannot find a single piece of documentation that will help me. Say I got a simple code to delete a row:
$count = $dbh->exec("DELETE FROM fruit WHERE colour = 'red'");
That will return affected rows, but how would I use prepared statements with that? Can use use $dbh->prepare
AND $dbh->exec
or query !?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它应该与任何其他语句相同:
PDO 语句 rowCount() 应该是你想要做的。
编辑
通过添加
->rowCount()
来修复,这将返回行数。无论查询是否出错,语句中的->execute
都会返回bool
、true
或false
。当然,所有这些信息都可以在 PDO 语句手册中轻松获得It should be the same as any other statement:
The PDO Statement rowCount() should be what you are looking to do.
EDIT
Fixed by adding the
->rowCount()
which will return the row count.->execute
in a statement will return abool
,true
orfalse
whether the query errored out or not. Of course all of this information is readily available at the PDO Statement Manual$dbh->prepare
返回一个PDOStatement
对象。然后,您调用$stmt->execute
来获取结果。PDO 手册中的更多信息
以下是手册中的示例:
$dbh->prepare
returns aPDOStatement
object. You then call$stmt->execute
to get the result.More info in the PDO manual
Here's an example from the manual: