Domesticated Brain

Fetching data from a MySql database and print result as a HTML table

 In this topic we fetch date from the table course in a database called testdb_domesticatedbrain

PHP Tutorials Course Table

Then display the result in the web browser as a html table

Open a php file called index inside the htdocs folder

First you have to create a connection for database server and select required data base


<!-- Following php code example shows how to open a database connection for MySQL database -->


$con = mysql_connect("localhost","root","root");
           die("Database Connection failed".mysql_error());
$db_select = mysql_select_db("testdb_domesticatedbrain", $con);
           die("Database selection failed".mysql_error());


$records = mysql_query("SELECT * FROM course");



<!-- This piece of PHP code defines the structure of the html table -->


<!DOCTYPE html>
        <title> Fetching data </title>


        <table width="400" border="2" cellpadding="2" cellspacing='1'>

           <tr bgcolor="#2ECCFA">
                     <th> CourseID</th>
                     <th>Course Title</th>

<!-- We use while loop to fetch data and display rows of date on html table -->


     while ($course = mysql_fetch_assoc($records)){

           echo "<tr>";
               echo "<td>".$course['courseID']."</td>";
               echo "<td>".$course['CourseTitle']."</td>";
               echo "<td>".$course['Duration']."</td>";
           echo "</tr>";





The output of the above PHP program we created shows as follows in the web browser

 PHP Tutorial Result As a HTML Table

If you have found  what you expected please comment and like on our facebook page