Aidyl Baylon
Aidyl Baylon

Reputation: 53

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.

Upvotes: 0

Views: 670

Answers (3)

user3835115
user3835115

Reputation:

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;

Upvotes: 0

Xavjer
Xavjer

Reputation: 9264

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

Upvotes: 2

Harshit
Harshit

Reputation: 5157

Use

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

And get the values as you normally does

$count = $row["getCount"];

Upvotes: 0

Related Questions