Count rows in mysql using PHP

Last updated on February 8th, 2022 at 01:06 pm

Click to rate this tutorial!
[Total: 4 Average: 3.8]

This script will help you how to find total number of rows in MYSQL table using php. Below we are giving examples of using old mysql_connect function in PHP and also new mysqli_connect . This will be helpful for developers who are still using older versions of PHP (not recommended update PHP soon 🙂 )

Using mysql_connect

<?php
$link = mysql_connect("localhost", "root", "");
mysql_select_db("mails", $link);
$result = mysql_query("SELECT * FROM mail", $link);
$a = mysql_num_rows($result);
echo "$a Rows\n";
?>

mysql_num_rows()will count the rows and assign it variable a.

Using mysqli_connect

<?php
$db = mysqli_connect('localhost','user','password'); //Don't forget to changes
mysqli_select_db($db,'database_name');                   //theses parameters
$result = mysqli_query($db,"SELECT * FROM table_name");
$a = mysqli_num_rows($result);
echo "$a Rows\n";
?>

mysqli_num_rows()will count the rows and assign it variable a.

Click to rate this tutorial!
[Total: 4 Average: 3.8]

Leave a Reply

Your email address will not be published.