PHP/PDO:对查询返回/受影响的行使用简单的预准备语句? [英] PHP/PDO: use simple prepared statement with query return/affected rows?
问题描述
我是PDO对象的新手,找不到一份对我有帮助的文档.假设我有一个简单的代码来删除行:
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'");
那将返回受影响的行,但是我将如何使用准备好的语句呢?可以使用$dbh->prepare
AND $dbh->exec
或查询!?
That will return affected rows, but how would I use prepared statements with that? Can use use $dbh->prepare
AND $dbh->exec
or query !?
推荐答案
它应与任何其他语句相同:
It should be the same as any other statement:
$stmt = $dbh->prepare("DELETE FROM fruit WHERE colour = ?");
$stmt->execute(array('red'));
$count = $stmt->rowCount();
PDO语句rowCount()应该是您所寻找的去做.
The PDO Statement rowCount() should be what you are looking to do.
编辑
通过添加->rowCount()
固定,它将返回行数.无论查询是否出错,语句中的->execute
都将返回bool
,true
或false
.当然,所有这些信息都可以在 PDO声明手册
Fixed by adding the ->rowCount()
which will return the row count. ->execute
in a statement will return a bool
, true
or false
whether the query errored out or not. Of course all of this information is readily available at the PDO Statement Manual
这篇关于PHP/PDO:对查询返回/受影响的行使用简单的预准备语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!