Reader Level:
ARTICLE

Display Data With HTML Table in PHP

Posted by Vinod Kumar Articles | PHP February 08, 2013
In this article I will explain how to use display data with HTML table in PHP
  • 0
  • 0
  • 9499

Introduction

When you display table data in a web browser from a MySQL database using a PHP application, it will be displayed unformatted.

Example

The following sample code displays data unformatted:

<?php

 

$con = mysql_connect("localhost","root","");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

mysql_select_db("smart", $con);

$result = mysql_query("SELECT * FROM Form");

while($row = mysql_fetch_array($result))

  {

  echo $row['Id'] . " " . $row['name']. $row['Mobile']. $row['email'];

  echo "<br />";

  }

 

?>

Output
Simple Fetch Data in PHP.jpg

And if you want to show your table data formatted, then you have many options for doing that, and HTML tables are one of them. One is a simple fetch of the table data from your database. Here I am using some HTML code to display the data or result. Whenever you want to use this article, you must first make a connection with your database, then you create the code for fetching the records from your table and then put your HTML code in a PHP script. Such as:

Example

<html>

<head>

//css

<style>

table

{

border-style:solid;

border-width:2px;

border-color:pink;

}

</style>

</head>

<body bgcolor="#EEFDEF">

<?php

$con = mysql_connect("localhost","root","");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("smart", $con);

 

$result = mysql_query("SELECT * FROM Form");

 

echo "<table border='1'>

<tr>

<th>Id</th>

<th>name</th>

<th>Mobile</th>

<th>email</th>

</tr>";

 

while($row = mysql_fetch_array($result))

  {

  echo "<tr>";

  echo "<td>" . $row['Id'] . "</td>";

  echo "<td>" . $row['name'] . "</td>";

  echo "<td>" . $row['Mobile'] . "</td>";

  echo "<td>" . $row['email'] . "</td>";

  echo "</tr>";

  }

echo "</table>";

 

mysql_close($con);

?>

</body>

</html>

Output

 HTML table in php.jpg

COMMENT USING

Trending up