How can conditional statements in PHP be effectively used to prevent errors when querying data that may not exist in a database table?

When querying data from a database table in PHP, it is important to use conditional statements to check if the data exists before trying to access it. This can prevent errors such as "Undefined index" or "Trying to get property of non-object" when the queried data is not found in the table. By using conditional statements, you can handle cases where the data may not exist gracefully and avoid potential errors.

// Query data from the database
$result = mysqli_query($conn, "SELECT * FROM table WHERE id = 123");

// Check if data exists before accessing it
if(mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_assoc($result);
    // Access the data safely
    echo $row['column_name'];
} else {
    echo "Data not found";
}