"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Display SQL Database Data in a PHP/HTML Table?

How to Display SQL Database Data in a PHP/HTML Table?

Published on 2024-11-12
Browse:921

How to Display SQL Database Data in a PHP/HTML Table?

Displaying Data from a SQL Database in a PHP/HTML Table

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.

Release Statement This article is reprinted at: 1729744437 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

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