What is the best practice for handling errors in PHP when using PDO->fetchAll?
When using PDO->fetchAll in PHP, it is important to handle any potential errors that may occur during the database query execution. One common practice is to wrap the fetchAll call in a try-catch block to catch any exceptions thrown by PDO. This allows you to handle the errors gracefully and provide appropriate feedback to the user.
try {
$stmt = $pdo->query("SELECT * FROM table");
$results = $stmt->fetchAll();
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}