How can the PHP code be modified to display all news entries in descending order based on the ID?
To display all news entries in descending order based on the ID, we can modify the SQL query in the PHP code to include an ORDER BY clause with the ID column in descending order. This will ensure that the news entries are displayed from the highest ID to the lowest ID.
<?php
// Connect to database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "news_database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Select all news entries in descending order based on ID
$sql = "SELECT * FROM news_entries ORDER BY id DESC";
$result = $conn->query($sql);
// Display news entries
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "ID: " . $row["id"]. " - Title: " . $row["title"]. " - Content: " . $row["content"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Related Questions
- How can PHP be used to extract an image from a zip file for display in a web application?
- Is it possible to have the uploaded images saved to an FTP server and the messages sent to an email inbox simultaneously in a PHP contact form?
- What are some key concepts in PHP that a beginner should understand before attempting to create a navigation system?