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

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

$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

$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.

Leave a Reply

Your email address will not be published. Required fields are marked *