本文介绍了PDO执行后获取行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图显示搜索后在数据库中找到的行数.

I am trying to show the number of rows found in the database after the searching.

这是我的代码:

$city =  $_POST['city'];
 $bloodType = $_POST['donorType'];

     $q = $db->prepare("SELECT count(*) FROM  `users` AS numusers WHERE `city` = :city  AND `bloodType` = :bloodType");
     $q->bindValue(":city",$city,PDO::PARAM_INT);
     $q->bindValue(":bloodType",$bloodType);
     $q->execute();

    while($row = $q->fetch(PDO::FETCH_ASSOC)){
    echo "<p align='center'><h5> There is/are <span class='red-text'>".$row['numusers']."</span> available donor(s) found.
    You must be a <b><a href='register.php'>registered user</a></b> to view their details.</h5></p>";
      }

那是我最后一次尝试.我收到此错误消息`通知:未定义的索引:numusers

That's the last try I did. And I get this error message`Notice: Undefined index: numusers

我该如何解决这些提示?

How do I solve that Tips ?

推荐答案

只需创建count(*)

SELECT count(*) AS numusers..

应该是

$q = $db->prepare("SELECT count(*) AS  numusers FROM `users` WHERE `city` = :city  AND `bloodType` = :bloodType");

这篇关于PDO执行后获取行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-29 19:03