What are the best practices for handling SQL statements from a file in PHP to ensure successful execution across multiple servers?

When handling SQL statements from a file in PHP to ensure successful execution across multiple servers, it is important to sanitize input to prevent SQL injection attacks and to use prepared statements for better performance and security. Additionally, using error handling to catch any potential issues with the SQL execution can help troubleshoot and resolve problems quickly.

// Read SQL statements from a file
$sqlFile = 'queries.sql';
$sqlStatements = file_get_contents($sqlFile);

// Separate SQL statements by delimiter
$delimiter = ';';
$sqlQueries = explode($delimiter, $sqlStatements);

// Execute each SQL statement using prepared statements
foreach ($sqlQueries as $query) {
    $stmt = $pdo->prepare($query);
    $stmt->execute();
    
    // Error handling
    if ($stmt->errorCode() !== '00000') {
        $errorInfo = $stmt->errorInfo();
        echo "Error executing SQL statement: " . $errorInfo[2];
    }
}