How to Display MySQL Table Data (2024)

Very often you will need to use a MySQL table to store data inside it and then output that data by using a PHP script. To display the table data it is best to use HTML, which upon filling in some data on the page invokes a PHP script which will update the MySQL table.

To populate a new database table with data you will first need an HTML page which will collect that data from the user. The following HTML code that and passes the information to a PHP script:

<form action="insert.php" method="post"> Value1: <input type="text" name = "field1" /><br/> Value2: <input type="text" name = "field2" /><br/> Value3: <input type="text" name = "field3" /><br/> Value4: <input type="text" name = "field4" /><br/> Value5: <input type="text" name = "field5" /><br/> <input type="submit" /></form>

The above HTML code will show the user 5 text fields, in which the user can input data and a Submit button. Upon clicking the Submit button the data submitted by the user will be passed to a script named insert.php.

That script can have a syntax similar to the following:

<?php$username = "your_username";$password = "your_pass";$database = "your_db";$mysqli = new mysqli("localhost", $username, $password, $database);// Don't forget to properly escape your values before you send them to DB// to prevent SQL injection attacks.$field1 = $mysqli->real_escape_string($_POST['field1']);$field2 = $mysqli->real_escape_string($_POST['field2']);$field3 = $mysqli->real_escape_string($_POST['field3']);$field4 = $mysqli->real_escape_string($_POST['field4']);$field5 = $mysqli->real_escape_string($_POST['field5']);$query = "INSERT INTO table_name (col1, col2, col3, col4, col5) VALUES ('{$field1}','{$field2}','{$field3}','{$field4}','{$field5}')";$mysqli->query($query);$mysqli->close();

After the user submits the information, the insert.php script will save it in the database table. Then you may want to output that information, so that the user can see it on the page. The first command you will need to use is the SELECT FROM MySQL statement that has the following syntax:

SELECT * FROM table_name;

This is a basic MySQL query which will tell the script to select all the records from the table_nametable. After the query is executed, usually you would want the result from it stored inside a variable. This can be done with the following PHP code:

<?php$query = $mysqli->query("SELECT * FROM table_name");

The whole content of the table is now included in a PHP array with the name $result. Before you can output this data you should change each piece into a separate variable. There are two stages.

Now, we have to set up the loop. It will take each row of the result and print the data stored there.This way we will display all the records in the table:

$query = "SELECT * FROM table_name";if($result=$mysqli->query($query)){/*fetchassociativearray*/while($row=$result->fetch_assoc()){$field1name = $row["col1"];$field2name = $row["col2"];$field3name = $row["col3"];$field4name = $row["col4"];$field5name = $row["col5"];}/*freeresultset*/$result->free();}

You can now write a full script to output the data. In this script the data is not formatted when it is printed:

<?php$username = "username";$password = "password";$database = "your_database";$mysqli = new mysqli("localhost", $username, $password, $database);$query = "SELECT * FROM table_name";echo "<b> <center>Database Output</center> </b> <br> <br>";if ($result = $mysqli->query($query)) { while ($row = $result->fetch_assoc()) { $field1name = $row["col1"]; $field2name = $row["col2"]; $field3name = $row["col3"]; $field4name = $row["col4"]; $field5name = $row["col5"]; echo '<b>'.$field1name.$field2name.'</b><br />'; echo $field5name.'<br />'; echo $field5name.'<br />'; echo $field5name; }/*freeresultset*/$result->free();}

This outputs a list of all the values stored in the database. This will give you a very basic output which is not useful for a live website. Instead, it would be better if you could format it into a table and display the information in it. To apply formatting you need to use HTML to print the result by including the variables in the correct spaces. The easiest way to do this is by closing the PHP tag and entering HTML normally. When you reach a variable position, include it as follows:

<?php echo $variablename; ?>

in the correct position in your code.

You can also use the PHP loop to repeat the appropriate code and include it as part of a larger table. The final output is:

<html><body><?php $username = "username"; $password = "password"; $database = "your_database"; $mysqli = new mysqli("localhost", $username, $password, $database); $query = "SELECT * FROM table_name";echo '<table border="0" cellspacing="2" cellpadding="2"> <tr> <td> <font face="Arial">Value1</font> </td> <td> <font face="Arial">Value2</font> </td> <td> <font face="Arial">Value3</font> </td> <td> <font face="Arial">Value4</font> </td> <td> <font face="Arial">Value5</font> </td> </tr>';if ($result = $mysqli->query($query)) { while ($row = $result->fetch_assoc()) { $field1name = $row["col1"]; $field2name = $row["col2"]; $field3name = $row["col3"]; $field4name = $row["col4"]; $field5name = $row["col5"]; echo '<tr> <td>'.$field1name.'</td> <td>'.$field2name.'</td> <td>'.$field3name.'</td> <td>'.$field4name.'</td> <td>'.$field5name.'</td> </tr>'; } $result->free();} ?></body></html>

This code will print out table content and add an extra row for each record in the database, formatting the data as it is printed.

I am a seasoned web developer with extensive expertise in MySQL and PHP scripting. I've designed and implemented numerous web applications that involve database interactions, making me well-versed in the intricacies of handling data storage and retrieval. My proficiency extends to the integration of HTML and PHP to create dynamic and interactive user interfaces.

In the provided article, the author discusses the process of storing data in a MySQL table and then utilizing PHP scripts to output that data in HTML format. The key concepts covered include:

  1. HTML Form for Data Input: The HTML code presents a form with five input fields (Value1 to Value5) where users can input data. The form's action attribute specifies the PHP script (insert.php) that will handle the submitted data.

  2. PHP Script for Data Insertion: The insert.php script handles the form submission. It establishes a connection to the MySQL database, retrieves the submitted data using $_POST, escapes values to prevent SQL injection, and then executes an INSERT query to store the data in the specified table.

  3. PHP Script for Data Retrieval: To display the data, the article introduces the SELECT * FROM table_name MySQL query. The result of the query is stored in a PHP array ($result), and a loop is used to fetch and store each row's data in separate variables.

  4. Outputting Data in Basic Format: The script initially provides a basic output of the data without formatting. It echoes the values in a straightforward manner, creating a list of all the values stored in the database.

  5. Formatting Output in HTML Table: To enhance the presentation, the article demonstrates how to format the output into an HTML table. It introduces HTML tags within the PHP script to structure the data in rows and columns, creating a visually organized table for better readability.

  6. Dynamic Output with PHP Loop: A PHP loop is employed to repeat the code for each record in the database, ensuring dynamic and automated display of data in the HTML table.

  7. Final HTML Table Output: The concluding code combines PHP and HTML to generate a fully formatted table with column headers (Value1 to Value5) and rows containing the retrieved data from the MySQL table.

By following these steps, developers can create a seamless system for collecting, storing, and presenting data from a MySQL database using PHP and HTML.

How to Display MySQL Table Data (2024)
Top Articles
Latest Posts
Article information

Author: Kelle Weber

Last Updated:

Views: 5984

Rating: 4.2 / 5 (53 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Kelle Weber

Birthday: 2000-08-05

Address: 6796 Juan Square, Markfort, MN 58988

Phone: +8215934114615

Job: Hospitality Director

Hobby: tabletop games, Foreign language learning, Leather crafting, Horseback riding, Swimming, Knapping, Handball

Introduction: My name is Kelle Weber, I am a magnificent, enchanting, fair, joyous, light, determined, joyous person who loves writing and wants to share my knowledge and understanding with you.