doulang6013 2012-07-04 04:34
浏览 21
已采纳

PDO两个查询,一个用于查找结果的其他行?

I currently do two queries when using prepared statements. One to fetch the results and another to fetch the number of found rows. I could previously do this with one query with mysql_num_rows. With larger queries i don't want to have to copy and paste the query and use COUNT every query. Is there a way to do this in one query like with mysql_num_rows?

 $connectdb->prepare("SELECT * FROM users WHERE username=:username");
        $connectdb->prepare("SELECT COUNT(*) FROM users WHERE username=:username");
    $query = $connectdb->execute(array(':username'=>$username));                                    
            $numrows = $query->fetchColumn();

             if($numrows!=0) {
               while(false !==($row = $query->fetch()))
               {
  • 写回答

3条回答 默认 最新

  • drqrdkfue521903877 2012-07-04 04:37
    关注

    The statement already has the row count: PDOStatement::rowCount()

    So use $query->rowCount(); and your first query only

    PS: your code doesn't look like it even would work. The correct one should be:

    $stmt = $connectdb->prepare("SELECT * FROM users WHERE username=:username");
    $stmt->execute(array(':username'=>$username));                                    
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?