In PHP, connecting to a MySQL database and displaying data from it into an HTML table can be achieved using the following steps:
<code class="php">$connection = mysql_connect('localhost', 'root', ''); //The Blank string is the password mysql_select_db('hrmwaitrose'); $query = "SELECT * FROM employee"; //You don't need a ; like you do in SQL $result = mysql_query($query); echo "<table>"; // start a table tag in the HTML while($row = mysql_fetch_array($result)){ //Creates a loop to loop through results echo "<tr><td>" . htmlspecialchars($row['name']) . "</td><td>" . htmlspecialchars($row['age']) . "</td></tr>"; //$row['index'] the index here is a field name } echo "</table>"; //Close the table in HTML mysql_close(); //Make sure to close out the database connection</code>
In this code:
This code provides a simple template for displaying data from a MySQL database into an HTML table in PHP. Please note that mysql_fetch_array is deprecated in PHP 7.0.0 and later, so you may need to use mysqli_fetch_array() instead.
The above is the detailed content of How to Display Data from SQL Database in PHP/HTML Table?. For more information, please follow other related articles on the PHP Chinese website!