What is the correct syntax for selecting the last 10 entries from a MySQL table in PHP?

When selecting the last 10 entries from a MySQL table in PHP, you can use the ORDER BY clause in combination with the LIMIT clause. By ordering the results in descending order based on a unique identifier (such as an auto-incremented ID), you can easily retrieve the last 10 entries.

// Connect to the 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);
}

// Select the last 10 entries from the table
$sql = "SELECT * FROM table_name ORDER BY id DESC LIMIT 10";
$result = $conn->query($sql);

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

$conn->close();