How can one avoid errors when fetching data from a SQL query in PHP, particularly when using associative arrays?

When fetching data from a SQL query in PHP using associative arrays, it is important to check if the key exists before accessing it to avoid errors. One way to do this is by using the isset() function to verify if the key exists in the array before trying to access it. This helps prevent "Undefined index" errors that can occur when trying to access non-existent keys in the array.

// Fetching data from a SQL query and using isset() to avoid errors
$result = $stmt->fetch(PDO::FETCH_ASSOC);

if(isset($result['column_name'])) {
    // Access the value of the 'column_name' key
    $value = $result['column_name'];
} else {
    // Handle the case where the key does not exist
    $value = null;
}