如何检查准备好的语句是否有结果
过去我会这样做:
$sql = 'SELECT * FROM customers WHERE customer_email="' . mysql_real_escape_string($_POST['customer_email']) . '" ';
$res = mysql_query($sql);
// if there are no hits...
if(mysql_num_rows($res) == FALSE) {
今天我正在做同样的事情,但是使用准备好的语句:
$stmt = $dbh->prepare("SELECT * FROM customers where customer_email = ? LIMIT 1");
if ($stmt->execute(array($_POST['customer_email']))) {
我准备好的语句的第二行 if($stmt... 是“如果这个查询得到结果”还是“如果无论结果如何都执行此查询,即如果它执行时没有错误”。
我想要解决的是使用准备好的语句如何执行相当于 mysql_num_rows() == FALSE 的操作?
谢谢!!
In the past I would do something like so:
$sql = 'SELECT * FROM customers WHERE customer_email="' . mysql_real_escape_string($_POST['customer_email']) . '" ';
$res = mysql_query($sql);
// if there are no hits...
if(mysql_num_rows($res) == FALSE) {
Today I am doing the same thing however with prepared statements:
$stmt = $dbh->prepare("SELECT * FROM customers where customer_email = ? LIMIT 1");
if ($stmt->execute(array($_POST['customer_email']))) {
The 2nd line of my prepared statement if($stmt... is that "if this query gets a result" or is it "if this query is executed regardless of results or not ie if it executes without error".
What I'm trying to work out is with prepared statements how do you do the equivalent of mysql_num_rows() == FALSE?
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
PDOStatement
的rowCount()
方法来获取返回的行数:或者,如果
rowCount()
被证明不可靠,你可以这样做:You can use the
rowCount()
method ofPDOStatement
to get the number of rows returned:Or, if
rowCount()
proves to be unreliable, you can do this:PDOStatement::rowCount()
返回受 DELETE、INSERT 或 UPDATE 语句影响的行数,而不是 select 查询返回的行数,对于选择查询我使用:
fetchAll(PDO::FETCH_OBJ);
和
回声计数($lignes);
PDOStatement::rowCount()
returns the number of rows affected by a DELETE, INSERT, or UPDATE statement and not the number of rows returned by select query,for select query I use :
fetchAll(PDO::FETCH_OBJ);
And
echo count($lignes);