Neha Neha - 2 years ago 72
PHP Question

Using MySQL Count function

I want to count all user records and display them in tables, I am trying this code code, It displays the record for one user only, I want to display records from all users.


$result1 = mysqli_query($con,"SELECT COUNT(user_id) as total FROM table-name where user_id=$u");

echo "<table border='1'>



<th>User ID</th>



while($row = mysqli_fetch_array($result1))
echo "<tr>";
echo "<td>" . $u . "</td>";
echo "<td>" . $row['total'] . "</td>";
echo "</tr>";
echo "</table>";

Answer Source

Try the following SQL Query:

SELECT `user_id`, COUNT(`user_id`) as `total` FROM `table-name` GROUP BY `user_id`;

Refer to the documentation of the GROUP BY clause.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download