What are some best practices for handling errors and debugging when using external SQL files in PHP?

When using external SQL files in PHP, it is important to handle errors and debug any issues that may arise. One best practice is to use try-catch blocks to catch any exceptions thrown by the database connection or query execution. Additionally, enabling error reporting and logging can help identify and troubleshoot any errors that occur.

try {
    $pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = file_get_contents('path/to/external/file.sql');
    $stmt = $pdo->prepare($sql);
    $stmt->execute();

    // Handle successful query execution here

} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}