How can DESC LIMIT be used to retrieve specific entries in a PHP database query?

To retrieve specific entries in a PHP database query using DESC LIMIT, you can use the DESC keyword to sort the results in descending order and the LIMIT keyword to specify the number of entries to retrieve. This can be useful for fetching the latest entries or a specific range of entries from a database table.

<?php
// Connect to database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Query to retrieve specific entries using DESC LIMIT
$sql = "SELECT * FROM myTable ORDER BY id DESC LIMIT 10";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "ID: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();
?>