Getting Count in PHP
Let's see how to get the counted number in our PHP script, as it's not that simple here.
Let's write the code that counts the number of users:
<?php
$query = "SELECT COUNT(*) FROM users";
$res = mysqli_query($link, $query) or die(mysqli_error($link));
$data = mysqli_fetch_assoc($res);
?>
In our case, the count
will end up in the variable $data. However,
this variable will be an array
of the following form:
<?php
var_dump($data); // ['COUNT(*)' => 6]
?>
To make the key in this array more
beautiful, you can rename our field in
the query using the as command:
<?php
$query = "SELECT COUNT(*) as count FROM users";
?>
After such renaming, in the variable
$data our count will already be
in the key 'count':
<?php
var_dump($data); // ['count' => 6]
?>
Count all users with a salary of 300.
Count all users with a salary of 300
or age of 23.