Total number of rows count using PHP and MYSQL table

Count the number of rows using two methods. You can use count() function . The count() function is used to count the elements of an array. The MySQL select (select dB table) query also used to count the table rows. It is a simple method to find out and echo rows count value.

To count the total number of rows using the PHP count() function, you have to create a MySQL database. Suppose that we have created a table "users" and want to count, how many users are inserted in the table.

MYSQL Database table ="users"

CREATE TABLE `users` (
  `id` int(50) NOT NULL,
  `uname` varchar(40) NOT NULL,
  `upassword` varchar(40) NOT NULL

In above table we will count the rows of this table using PHP count() function and mysql select query . Now insert the data into the table .

INSERT INTO `users` (`id`, `uname`, `upassword`) VALUES
(1, 'admin', 'admin@123'),
(2, 'admin2', 'admin@123'),
(3, 'admin3', 'admin@1234'),
(4, 'admin4', 'admin@678'),
(5, 'admin5', 'admin@679');

In above data that we have inserted into users table, there are 5 rows and When we will count the rows, the result should be 5 .
Now create a PHP script.

Count table rows


$databaseHost = 'localhost';   //your db host 
$databaseName = 'admin';  //your db name 
$databaseUsername = 'root';    //your db username 
$databasePassword = '';//   db password 

$mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName); 

if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();
$sql="select count('1') from users";
echo "


"; mysqli_close($mysqli); ?>

In above PHP script we have used the count() function . We have used = count(1) function also we can use count(*) replace count(1) because. COUNT(*) counts the number of rows. Since both 0 and 1 are non-null values, COUNT(0)=COUNT(1) and they both will be equivalent to the number of rows COUNT(*). That is a different concept, but the result produced will be the same. A MySQL select query also used in the PHP rows count script. We have defined the table named "users" in the MySQL select query. This will help select the table. We have used the mysqli_fetch_array function to fetch a result row as an associative array, a numeric array, or both.

At the end, we did the echo count value. It will produce row count 5.

Row count using PHP mysqli_num_rows(); function

PHP provided a mysqli_num_rows(); function that hepls to count the mysql table rows .

Count table rows


$databaseHost = 'localhost';   //your db host 
$databaseName = 'admin';  //your db name 
$databaseUsername = 'root';    //your db username 
$databasePassword = '';//   db password 

$mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName); 
$sql = "SELECT * FROM users";
$mysqliStatus = $mysqli->query($sql);
$rows_count_value = mysqli_num_rows($mysqliStatus);
echo $rows_count_value; 


In the above table row count script.

1. Create a connection of database.

2. Select the table using MySQL select query.

3. count the table row using mysqli_num_rows() function .

4. Display the row count value using echo.

Please Share

Recommended Posts:-