How to Display a MySQL Table of a Database Using PHP



PHP


In this article, we will show how to display a table that you have in a MySQL database.

To do this, first we have to connect to the database of the MySQL server and select the table we would like to display.

Once we do this, we can display the table with PHP using the HTML elements to place information in tabular form.

PHP Code

The full code to choose a table from a MySQL table and to display it is:



The first block of code is where you place in your servername, user name, password, database name, and table name of the MySQL server, database, and table, that you want to connect to.

The second block of code connects you to the MySQL server and the database of in the MySQL server that you want to connect to. The line mysql_connect($host, $user, $password); connects you to the MySQL server and the line @mysql_select_db($database) or die("Unable to select datbase"); connects you to the database in the server which you would like to connect to. If the program can't connect successfully to the database, the line "Unable to select database" will appear.

The third block of code connects you to the table in the database which you would like to display. This connection is stored in the $result variable. We will use this variable in the next block of code to print out the table in table form.

The fourth block of code prints outs all the rows in the table. This uses standard HTML elements to create the table.

  • The first line print "<table width=540 border=1>\n"; creates the table and gives it a width of 540 and a border of 1. If you like you can increase or decrease the width of the table, to personal preference. If you would like the table to have a border, the line border=1 must be appended. If you do not want a border, you can either change border=1 to border=0, or you can just remove the line completely. If you would like a thicker border, you can increase border=1 to a larger value, such as border=2 or a greater value, according to your preference.
  • The second line while ($get_info= mysql_fetch_row($result)) uses a while loop to retrieve all of the rows of the MySQL table. The $result variable has the value of the selected table and the mysql_fetch_row function retrieves all of the rows of the table and stores it in the $get_info variable.
  • The lines $images_field= $row['images']; $Type= $row['Type']; $price= $row['Price']; retrieve the values of the images column, the Type column, and the Price column. With this data, now we can show these columns. For your table, substitute these column names with the names of the columns of your table.
  • The line $image_show= "/images/$images_field"; specifies the path of images. When we put images in our MySQL tables which are displayed, we don't place the whole images in the database. All we do in the MySQL table is specify the image name and type. Using PHP code, we then specify the complete path to the image in order to display. This line specifies the complete path to the images, so that they can be displayed. These images are located in the /images/ folder. If they are located somewhere else, then specify that path. If you're not putting images in your table, this line can be disregarded.
  • The line print "<tr>\n"; creates a new row in the table. Every time a while statement loops back and starts from the beginning, a new row is created each time until there are no more rows left in the table.
  • The print "\t\n"; and print "\n"; lines create each of the columns for each row of data. If you have 3 columns which you want to display, then you have to have three of these statements. If you have 7 columns, then you'll have to have 7 of these lines. It all depends on the amount of columns in the table. In the above example, we have three of them. They display the images column, the Type column, and the Price column. If you want to align the data from the column in the center of it, you would add the line <div align=center></div> to it.
  • The remaining line print "</table>\n"; closes the table, respectively.

And this is how a table can be selected and displayed with using PHP.


Related Resources

How to Create a MySQL Table Using PHP

How to Select a Table from a MySQL Database

How to Check if a MySQL Table Exists Using PHP

How to Show All Tables of a MySQL Database Using PHP

How to Delete a MySQL Table Using PHP

How to Copy a Table Definition in MySQL Using PHP




HTML Comment Box is loading comments...