What are some recommended best practices for handling missing records in PHP database queries?

When handling missing records in PHP database queries, it is recommended to check if the query returned any results before trying to access the data. This can prevent errors from occurring when trying to access properties of a non-existent record. One way to handle this is to use conditional statements to check if the query returned any rows before attempting to fetch the data.

// Execute the database query
$result = mysqli_query($connection, "SELECT * FROM table WHERE id = 123");

// Check if any rows were returned
if(mysqli_num_rows($result) > 0) {
    // Fetch the data from the query result
    $row = mysqli_fetch_assoc($result);
    
    // Access the data from the fetched row
    echo $row['column_name'];
} else {
    // Handle the case where no records were found
    echo "No records found";
}