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
Johnathan Ward is an experienced developer and consultant that writes tutorials to help other developers. In his day job, he is an IBM Watson Consultant with several years of experience deploying and customizing Watson Explorer solutions.