Last updated on April 8th, 2022 at 12:51 pm

This tutorial will show you how to display mysql tables using for loop instead of while in php. We are using mysqli_num_rows to get the number of rows and using mysqli_fetch_row to fetch each row.

Old PHP Version using mysql_connect

//please dont forget to connect to mysql
$objDB = mysql_select_db('my_db');
$qry_acct='SELECT * From My_test_table';
$result_acct=mysql_query($qry_acct) or die ('Error Query ['.$qry_acct.']');
$num_acct = mysql_num_rows($result_acct);
for($i=0;$i<s $num_acct;$i++)
//My Column is one of the column in my TABLE
echo mysql_result($result_acct,$i,'My Column').'<br>';

New PHP Version using mysqli_connect

$servername = "localhost";
$username = "";
$password = "";
$conn = mysqli_connect($servername, $username, $password, $db);
$qry = "SELECT * FROM $tablename";
$result = mysqli_query($conn,$qry);
$num_acct = mysqli_num_rows($result);
$data= mysqli_fetch_row($result);
echo $data[$i]."<br>";

One thought on “How to display mysql table using for loop in php”

Leave a Reply

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