Last updated on November 7th, 2024 at 07:30 am
This script provides guidance on determining the total number of rows in a MySQL table using PHP. Below, we present examples utilizing the deprecated mysql_connect function as well as the newer mysqli_connect function. The code below assist in determining the number of rows in a MySQL database using PHP.
This information will be beneficial for developers who continue to work with older versions of PHP, although it is advisable to update to a more recent version 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.