【问题标题】:count the number of rows in a query计算查询中的行数
【发布时间】:2015-11-17 15:30:22
【问题描述】:

我在我的工作页面中分离了这个功能。

public function countRow(){
        $id = $_SESSION['id'];
        $num = 1;
        $query = "SELECT count(*) from `auditsummary` where bizID=? AND statusID=?";
        $sql = $this->db->prepare($query);
        $sql->bindParam(1,$id);
        $sql->bindParam(2,$num);
        $sql->execute();


    }

我在这个函数中真正想要做的是计算查询结果的行数,但我不知道该怎么做,也不知道如何返回值。

【问题讨论】:

    标签: php mysql function count pdostatement


    【解决方案1】:

    当您使用 PDOStatement 进行查询时,在执行后,您可以使用

    $count = $sql->rowCount();
    

    更多信息: http://php.net/manual/en/pdostatement.rowcount.php

    要返回结果,您可以这样做:

    return $count;
    

    相关信息: http://php.net/manual/en/function.return.php

    【讨论】:

      【解决方案2】:

      使用

      $query = "SELECT count(*) AS getCount from `auditsummary` where bizID=? AND statusID=?";
      

      并像往常一样获取值

      $count = $row["getCount"];
      

      【讨论】:

        【解决方案3】:

        我是这样做的:

        $count = "SELECT * FROM yourtable WHERE x='x' and y='y'";
        
        $result = $dbconn->prepare($count);
        $result->execute();
        $t_count = $result->rowCount();
        
        echo $t_count;
        

        【讨论】:

        • $pages 是从哪里来的?
        猜你喜欢
        • 2011-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-09
        • 2013-10-03
        相关资源
        最近更新 更多