dig_abacus asked this 7 years ago

MySQL php mysqli How to use the result of SELECT COUNT(*) mysqli_query

Hi, I am using

$result = mysqli_query($conn,"SELECT COUNT(*) FROM prod_catalogue WHERE cat_id ='".$catid."' " ) or die(mysqli_error($this->dblink));

How do i get the actual count from result because $result doesn't return a count.


mkbaines 7 years ago
8 likes

$result will have a single row with count. You can assign the count a column name so it easy to reference it,

$query1 = mysqli_query($conn,"SELECT COUNT(*) AS mycount FROM prod_catalogue WHERE cat_id ='".$catid."' " ) or die(mysqli_error($this->dblink));

$res = mysqli_fetch_object($query1);

$count = $res->mycount;

Vikas 7 years ago
3 likes

1. Give an alias for count (*)

SELECT COUNT(*) as CNT FROM prod_catalogue WHERE cat_id  .............

 

2. Then fetch the query result:

  $ROW = mysqli_fetch_object($result);

3. Count will be the first element in the result set

$ROW->cnt:0