How can PHP scripts be modified to display database output in reverse order in an HTML table?
To display database output in reverse order in an HTML table using PHP, you can modify your SQL query to include an ORDER BY clause with a DESC keyword to sort the results in descending order based on a specific column. This will ensure that the data is displayed in reverse order in the HTML table.
<?php
// Connect to database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// SQL query to retrieve data from database in reverse order
$sql = "SELECT * FROM table_name ORDER BY column_name DESC";
$result = $conn->query($sql);
// Display data in HTML table
echo "<table>";
echo "<tr><th>Column 1</th><th>Column 2</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row['column1'] . "</td><td>" . $row['column2'] . "</td></tr>";
}
echo "</table>";
// Close connection
$conn->close();
?>
Keywords
Related Questions
- What are some troubleshooting steps for identifying the root cause of a failed database update in PHP?
- What are some best practices for copying images to a different folder within a web space using PHP?
- What are the best practices for naming checkboxes and passing their values in PHP to ensure proper data handling?