Home >Database >Mysql Tutorial >How to Display SQL Database Data in PHP/HTML Tables?
Displaying SQL Database Data in PHP/HTML Tables
Question:
How can I display data from a SQL table on a HTML or PHP table?
Background:
You have a database on MySQL and want to display data from the "employee" table on a web page.
Solution:
PHP provides functionality to establish a connection to a MySQL database. Here's a code snippet that demonstrates how to achieve this:
<code class="php">$connection = mysql_connect('localhost', 'root', ''); // Password is left blank mysql_select_db('hrmwaitrose'); $query = "SELECT * FROM employee"; // SQL statement $result = mysql_query($query); echo "<table>"; // Start HTML table while ($row = mysql_fetch_array($result)) { echo "<tr><td>" . htmlspecialchars($row['name']) . "</td><td>" . htmlspecialchars($row['age']) . "</td></tr>"; } echo "</table>"; // End HTML table mysql_close(); // Close database connection</code>
In the while loop, each row of the SQL query result is converted into a table row in HTML.
Note:
The above is the detailed content of How to Display SQL Database Data in PHP/HTML Tables?. For more information, please follow other related articles on the PHP Chinese website!