PHP code to display MySQL data in an HTML table

By | July 19, 2016

A simple HTML form containing fields – Name, Address Line1, Address Line2, and E-mail. On submitting the form, the data is inserted into MySQL table using PHP code. We will iterate over each record present in the table to display MySQL data in an HTML table.




<?xml version="1.0" encoding="ISO-8859-1"?>
<html xmlns="">

<h3> Program to collect the customer-information </h3>
<form action ="Display.php" method="get">
<table border="5">
<td> Enter Name:</td>
<td> <input type="text" name="name"> </td>
<td> Enter Address Line1: </td>
<td> <input type="text" name="address1"></td>
<td> Enter Address line2: </td>
<td> <input type="text" name="address2"> </td>
<td> Enter Email-id: </td>
<td> <input type="text" name="email"> </td>
<td> </td> </tr>
<tr> </tr>
<td> <input type="submit" value="Submit"></td>

<td> <input type="Reset" value="Reset"></td>
<a href="<a href="" target="_blank" rel="noopener">Search.html</a>"> To search click here </a> </td>




The following image shows how the UserData.html looks on the browser:
HTML form to insert data in to MySQL table


Suggested Read:



<head><title> Display.php </title></head>
<body bgcolor="aabbcc">





define('DB_SERVER', 'localhost:3306');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'root123');
define('DB_DATABASE', 'customers'); //where customers is the database

$query= "insert into address values('$name1','$address1','$address2','$email'); //to insert input records into a table - address
$enter= mysqli_query($db,$query);

$query="select * from address"; // Fetch all the records from the table address

<h3> Page to display the stored data </h3>

<table border="1">
<th> NAME </th>
<th> ADDRESS Line1 </th>
<th> ADDRESS Line2 </th>
<th> EMAIL-id </th> </tr>

<?php while($array=mysqli_fetch_row($result)) ?>
<td><?echo $array[0];?></td>
<td><?echo $array[1];?></td>
<td><?echo $array[2];?></td>
<td><?echo $array[3];?></td>

<?php endwhile; ?>
<?php mysqli_free_result($result); ?>
<?php mysqli_close($db); ?>


The following image shows how the Display.php looks on the browser:

PHP code to display MySQL data in an HTML table

display MySQL data in an HTML table

How to execute this code?

  1. You need to copy files UserData.html and Display.php to a PHP server. You can use wampserver software which provides an integrated environment to run PHP files along with the MySQL database server.
  2. Next, open this HTML file in the browser via localhost URL. Usually http://localhost/UserData.html
  3. Enter user details and submit the data.
  4. You should be able to see the results displayed on the same browser via Display.php file.
Suggested articles for you:

6 thoughts on “PHP code to display MySQL data in an HTML table

  1. blankArya Dhorajiya

    hi, i am getting the error

    Parse error: syntax error, unexpected ‘SELECT’ (T_STRING) in C:\xampp\htdocs\Display.php on line 23

    1. blankRavi Bandakkanavar Post author

      Hi Arya,

      It’s showing the error at the line
      $query="select * from address";

      This means there is some syntax issue on this line. Check for double quotes.

  2. blankGaneswari

    It helps.But the above code has a mid error in file “display.html” in line 40,41,42,43

    Instead of this,use

    Pls correct it and post.Thank u so much

    1. blankRavi Bandakkanavar Post author

      Hi Ganeswari,

      This is a tested code. There is nothing wrong with the statement.
      echo $array[0]

      There should be some data in the table for this to work. Is there data in the table?


Did it help? Would you like to express?