简体   繁体   中英

count the number of rows in a query

i have this function separated in my working page.

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();


    }

what i'm really trying to do in this function is to count the number of rows that are results of the query but i don't know how to do it and also how to return the value.

As you use a PDOStatement for your query, after the execute, you can use

$count = $sql->rowCount();

More information: http://php.net/manual/en/pdostatement.rowcount.php

And to return the result, you can just do:

return $count;

Information for this: http://php.net/manual/en/function.return.php

Use

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

And get the values as you normally does

$count = $row["getCount"];

Here's how I do it:

$count = "SELECT * FROM yourtable WHERE x='x' and y='y'";

$result = $dbconn->prepare($count);
$result->execute();
$t_count = $result->rowCount();

echo $t_count;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM