When working with a MySQL database, there may be instances where you need to display table data on a PHP/HTML table. This can be achieved through the following steps:
";
// Retrieve each row of data from the query result
while ($row = mysql_fetch_array($result)) {
// Create a new table row for each employee record
echo "" . htmlspecialchars($row['name']) . " " . htmlspecialchars($row['age']) . " ";
}
// Close the HTML table
echo "";
// Close the MySQL connection
mysql_close();
?>
In this script, the mysql_connect function establishes a connection to the MySQL database, while mysql_select_db selects the specific database to work with. The mysql_query function executes the SQL query to retrieve data from the 'employee' table.
The mysql_fetch_array function retrieves each row of data from the query result and assigns the values to an array. The htmlspecialchars function is used to escape any special characters in the data returned from the database to prevent potential security vulnerabilities.
The echo statements output the HTML code to create a table with the employee data. The while loop continues until all rows from the query result have been processed.
Please note that the mysql_fetch_array function is deprecated in PHP versions 5.5.0 and above, so it's recommended to use mysqli_fetch_array instead for improved security.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3