How can array_key_exists be effectively used in PHP to check for existing data in a MySQL result?

When fetching data from a MySQL result in PHP, it is important to check if a specific key exists before attempting to access it to avoid potential errors. The array_key_exists function in PHP can be effectively used to check for existing data in a MySQL result by verifying if the key exists in the fetched row before using it.

// Assume $row is the fetched row from a MySQL result
if(array_key_exists('key_name', $row)) {
    // Key exists, access the data
    $value = $row['key_name'];
    // Use the value as needed
} else {
    // Key does not exist, handle accordingly
    echo "Key does not exist in the fetched row.";
}