Are there any potential pitfalls when selecting the last ID from a table in PHP?

When selecting the last ID from a table in PHP, one potential pitfall is that the table may not have any records yet, resulting in an error when trying to retrieve the last ID. To solve this, you can check if there are any records in the table before attempting to retrieve the last ID.

// Check if there are any records in the table
$result = $mysqli->query("SELECT id FROM your_table");
if($result->num_rows > 0) {
    // Retrieve the last ID from the table
    $result = $mysqli->query("SELECT id FROM your_table ORDER BY id DESC LIMIT 1");
    $row = $result->fetch_assoc();
    $last_id = $row['id'];
    echo "Last ID: " . $last_id;
} else {
    echo "No records found in the table.";
}