【问题标题】:Count does not work when I rewrote my code with PDO当我用 PDO 重写我的代码时,计数不起作用
【发布时间】:2015-01-02 14:34:43
【问题描述】:

从 mysql 更改为 pdo 后,我的计数无法正常工作。我不知道该怎么办。

<tr>
    <td style="width: 125px">
        <a href="SystemsTechsPages/xgrh/xgrhCompleted1.php" target="_top">xgrh</a>
    </td>
    <td style="width: 125px" align="center">
        <a href="SystemsTechsPages/xgrh/xgrhCompleted1.php" target="_top">
        <?php 

        $stmt = $db->prepare("
            SELECT COUNT(*) FROM WHERE requests status='Completed' AND compDT=Curdate() AND compUser='xgrh'
        ");  
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
            echo $row['COUNT(*)'];  
        }  
        ?>
        </a>
    </td>
</tr>

【问题讨论】:

  • SELECT COUNT(*) as count FROM your_table 然后做echo $row['count']; 顺便说一句你的桌子不见了。
  • 我把表名从 MySQL 改成 PDO 搞砸了忘记放表名

标签: php html mysql pdo


【解决方案1】:

您应该在获取之前调用$stmt-&gt;execute()。准备好的 STMT 不会发送到服务器,您应该使用执行发送它。

【讨论】:

    【解决方案2】:

    你应该改变你的查询:

    SELECT COUNT(*) AS rows_cnt FROM table WHERE  status='Completed' AND compDT=Curdate() AND compUser='xgrh'
    

    然后:

    并像这样使用它:

    $stmt->execute();
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
    { 
       echo $row['rows_cnt'];  
    }
    

    【讨论】:

      【解决方案3】:

      首先,确保查询正确:

      缺少表名

      SELECT COUNT(*) FROM (what happened here? no table name)
      
      WHERE status='Completed' 
      AND compDT=Curdate() 
      AND compUser = 'xgrh'
      

      其次,您可以只使用别名来更好地访问列名:

      <?php 
      // prepare
      $stmt = $db->prepare("
          SELECT COUNT(*) AS total FROM table_name 
          WHERE status='Completed' 
          AND compDT = Curdate() 
          AND compUser = 'xgrh'
      ");
      $stmt->execute(); // execute
      $result = $stmt->fetch_assoc(PDO::FETCH_ASSOC);
      $count = $result['total'];
      echo $count;
      
      ?>
      

      【讨论】:

      • 哈哈! (what happened here? no table name) 好收获。我不会回答,已经太多了。 +1 我确实发表了评论。
      • @Fred-ii- 实际上,当我滚动时,我的眼睛变大了,这是 WTF!?大声笑:D
      • 你的 (what happened here? no table name) 引起了我的注意,这是我在 OP 的问题下编辑我的评论时。我也拍了两下。太好了,现在我有鞭子了,哈哈
      • 糟糕,我有表名,我在更换 MySQL 时忘记添加它。感谢所有帮助我开始了解 Pdo 我会试一试,明天将 Answere 标记为正确
      【解决方案4】:

      您可以像这样在 PDO 中获取行数:

      $count = $stmt->rowCount();
      

      【讨论】:

      • PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object. 我在查询中没有看到任何这些。
      • @CappY 正要说同样的话。
      • @CappY 嗯,我其实不知道。我一直都在使用它...我刚刚阅读了...SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.的警告我只使用了我工作的一个主机,幸运的是该规则似乎适用....但我最好重新考虑一下,因为“可能或可能无法工作”通知。
      • @Rasclatt 不要难过,你并不孤单。我上周自己学会了这一点,哈哈
      • @Fred-ii- 天哪,我在任何地方都认真地使用它!这只是增加了我需要修改的大量工作!!非常感谢!!! :D
      猜你喜欢
      • 2014-09-06
      • 2017-09-28
      • 1970-01-01
      • 1970-01-01
      • 2021-06-11
      • 1970-01-01
      • 1970-01-01
      • 2021-10-27
      • 2023-03-06
      相关资源
      最近更新 更多