In the footsteps Forming queries to the table data, comparing and outputting the sum Created a query to the database, it is processed if run directly in phpmyadmin. How to integrate it into PHP

SELECT c.`id` , ( SELECT COUNT( * ) FROM `items_comments` WHERE `reply_to` = c.id ) AS сount FROM `items_comments` c WHERE c.`reply_to` = 0 

Actually the request itself, but not processed, if you insert it like this:

 public function ReplyCount($id) { global $mysql, $langArray; $mysql->query(" SELECT c.`id` , ( SELECT COUNT( * ) FROM `items_comments` WHERE `reply_to` = c.id ) AS сount FROM `items_comments` c WHERE c.`reply_to` = '0' "); return true; } 

Where ReplyCount is the value to be output. How to make it?

  • What is the $ mysql object? - cheops
  • If you understand correctly global - Nikolay
  • Not really, the global keyword declares a global object so that you can access it from a function. But what is the class of this object? - cheops

1 answer 1

Try this:

 public function ReplyCount($id) { global $mysql, $langArray; $result = $mysql->query(" SELECT c.`id` , ( SELECT COUNT( * ) FROM `items_comments` WHERE `reply_to` = c.id ) AS сount FROM `items_comments` c WHERE c.`reply_to` = '0' "); $row = $result->fetch_array(MYSQLI_ASSOC); return $row['count']; } 
  • It does not work, but it seems to me that the snag is just in the wrong indication of c.id - I do not really understand what it is. - Nikolay