Giel Evens Giel Evens - 6 months ago 12
MySQL Question

Count how many rows you have in your db with PHP

Hellow,

I want to count how many rows i have in a table. I got a table (workstations) in my mysql database (phpmyadmin). I want to print it out, so ik can see how many workstations their are "active" in my environment

I've read many blogs about this, but all the things they propose are not working for me.

Thanks in advance!

Answer

Use COUNT(*)

You can see here: http://dev.mysql.com/doc/refman/5.7/en/counting-rows.html

SELECT COUNT(*) FROM table_name;

ex:

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$query = "SELECT COUNT(*) FROM table_name";
$result = mysqli_query($mysqli,$query);
$rows = mysqli_fetch_row($result);
echo $rows[0];
Comments