How to Return MySQL Results to a Table

You should not use this code on a production website.

Warning: This tutorial uses old techniques. It is insecure and will leave your server vulnerable to SQL Injection attacks.This tutorials also uses mysql_ functions that are no longer support. For updated tutorials look for a PDO or MySQLi tutorial.This post will be delete or revised in the future.

This question has been asked several times lately and I wanted to clear this up. This is a very easy concept once you get it down. In order to display the results from a MySQL query into a table will will store the results in an array and then echo out each row in a while loop.

First off. I am going to use a table that I created in the past tutorials on this site.

The table contains the following fields:

ID – auto generated integer
FName – First Name, varchar
LName – Last Name, varchar
PHON – Phone number, varchar

Create the table and populate some test data if you haven’t done so already. If you don’t know how to do this please visit some of our other MySQL tutorials.

Create your database connection

<?php
//create a database connection
mysql_connect("localhost","your username","password") or die("Error: ".mysqlerror());
//select your database
mysql_select_db("your db");
?>

Next we need to setup the table. We will add the column headings.

<?php
//create a database connection
mysql_connect("localhost","your username","password") or die("Error: ".mysqlerror());
//select your database
mysql_select_db("your db");
?>

<html>
<body>

<table border=1>
  <tr>
   <th>ID</th>
   <th>First Name</th>
   <th>Last Name</th>
   <th>Phone Number</th>
  </tr>

As you can see above we ended our PHP code and create an HTML table. The TR tage is used to defined a table row and the TH tag is used for Table Headings.

In php you can “break” out of code and execute HTML and then continue writing php code. Do do this you simply end your php code with then start a new

Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join the TeamTutorials mailing list and get the latest tips, tricks, and special discounts for members only.

Thanks! Please click the link in the email we just sent you to confirm.

  • Ryan

    This is great to get the loop started however each entry is repeated.
    Really easy to get started… just need to work out how to stop the repeatition of the data.

  • Red Servo

    This is my code… What is wrong with this? I am using Linux Mandriva and phpMyAdmin.. Please Analyse this:

    Name
    Age
    Date
    Comment

    <?php }

  • Red Servo

    This is my code… What is wrong with this? I am using Linux Mandriva and phpMyAdmin.. Please Analyse this:
    /*

    Name
    Age
    Date
    Comment

    <?php }

    */

  • Red Servo

    Sorry My whole code does not appear….

  • Hi,Thanks for sharing this nice info related good and important topic to Return MySQL Results to a Table.

  • The code looks quite a mess..

  • I think this code have missing line.

  • I agree generic.It looks quite a mess.

  • I think the plugin broke this code

  • Code should be correct now.

  • yudi

    Thanks for the code.

  • mehmet

    i can insert data in mysql by php but i want add two releation table with same id fore example user will have two table firstli he add information in a table from a form after that he can add any information in second table in different time.how can i use same id in two table andthats id can be unique for thats users?

  • I am getting the following error.

    Parse error: syntax error, unexpected ‘;’, expecting T_PAAMAYIM_NEKUDOTAYIM in /home/vpgonlin/public_html/pcf/showdata.php on line 21

    Below is my Code can someone help me fix this?

    ID
    Company

  • //
    //
    //

  • Anyway I could not post my code, but I have it exactly as the example code above. Line 21 I am getting an error.

    • There us a problem with the code since we upgraded. For some reason the quotes aren’t displaying properly. It would also help if your error was I English but my guess is you forgot a semicolon or have a syntax error

  • chris

    Thanks the code works. there is one problem. when the user decreases the size of the page the table jumbles up to continue to fite all of the info inside. Is there any overflow code that can be used to fix the problem?

  • this is not working! help!
    i have created a table exactly the same as the above (with column titles as ID, Fname, Lname and PHON) and named it TestTable

    I have copied and pasted the above code, changed all " into an actual ” but when I open the page, I get a blank page.

    if i remove the second php codes, i see the html table… what is wrong? thanks.

    • rohan

      never mind, i found the missing “>?” before the “”

  • Basu

    I’ve installed Wampserver 2.1. I can see its running with all of its components. I can practice queries in the MySQL Console individually. I am creating php pages in dreamweaver. My question is, “how could I been able to connect these pages to the WampServer so that I can run these these pages as well as can execute mysql db from these php pages. Plzzz reply.
    Thanks in advance.

  • A quick question for you guys? Based on the above script, how would one change the code to facilitate a search on a particular “PHONE” number (not name)

  • Wow,its great article.
    Thanks mate.

  • Prashant kyada

    thanks to so much..
    this tutorial is very use 4 me.

  • Jonas

    Hi

    Thanks so much..this helpt me alot 🙂 but how can i make the row get dif colors , its kinda bluru whith the same color on eatch row,

    Br
    Jonas

  • White Jasmine

    Hey..this tutorial seems to be similar to what i need
    you see for what i’m coding for..based on a term (ID) a person types into a search in php, i want php to search a mysql database and load the associated info into an HTML database within a seperate html file…and everytime someone searches (adds) a new record…i want the html table to refresh with the new info

    so could u tell me how to do that….thanks

    • White Jasmine

      Hey..this tutorial seems to be similar to what i needyou see for what i’m coding for..based on a term (ID) a person types into a search in php, i want php to search a mysql database and load the associated info into an HTML table* within a seperate html file…and everytime someone searches (adds) a new record…i want the html table to refresh with the new info
      so could u tell me how to do that….thanks

      *sorry i meant table NOT database

  • Joseph Amanko

    please how do i upload a file with text content separated with spaces in php?