How can one effectively handle errors in PHP using functions like mysql_error()?

When handling errors in PHP using functions like mysql_error(), it is important to check for errors after executing database queries and handle them appropriately. One common approach is to use conditional statements to check for errors and display error messages or log them for further investigation. By incorporating error handling logic in your code, you can ensure that potential issues are identified and addressed promptly.

// Execute a MySQL query
$result = mysqli_query($connection, "SELECT * FROM users");

// Check for errors
if (!$result) {
    // Display error message
    echo "Error: " . mysqli_error($connection);
    
    // Log the error for further investigation
    error_log("MySQL Error: " . mysqli_error($connection));
} else {
    // Process the query result
    while ($row = mysqli_fetch_assoc($result)) {
        // Do something with the data
    }
}

// Close the database connection
mysqli_close($connection);