How to Select and Display mysql table data using Select Query

PHP Select query is used to retrieve data from mysql table and display using the echo() php function.

Here is a  simple PHP code  that  can  be embedded in HTML and can be used to display table data.

If we have a table customers that has the following fields

Customer Id,Firstname,SecondName and Phone Number

Those are the values that we need retrieved from the database and displayed by php in form of a table

1.First we need to connect to a mysql database.See my previous post about connecting to a mysql database here

here is the code to connect to mysql database

// Connects to your Database
if(!defined('INCLUDE_CHECK')) die("not allowed to execute directly");



$password="password"; // Mysql password


$link=mysql_connect($host,$username,$password) or die(mysql_error());

mysql_query("SET names UTF8");


2.Once connected.,excute the select command

$sql="SELECT*FROM Customers";


The above code will select all the table data name “Customers” and assign the result value to a Php Variable $rs

3.The next step is to assing the values returned into a multidimensionl array


4.Once all the values have been assigned to a multidimensional array they can be displayed using a while loop


while ($array=mysql_fetch_row($rs))

echo '<tr style="background-color:#CCCCCC">';
echo'<td class="a-center"><font size="2">'.$row[0].'</font></td>';

echo'<td> <font size="2">'.$row[1].'</font></td>';

echo' <td><font size="2">'.$row[2].'</font></td>';
echo'<td><a href="#"><font size="2">'.$row[3].'</font></a></td>';

echo '</tr>';


About the Author: Eric K

Kenyan Developer and Blogger with Interests in Java,Javascript,Node.js, Wordpress, Android, PHP and Making Money Online. Solving world problems through programming is my passion. Hire me: Email :