What is the recommended method for processing and executing SQL queries from a .sql file in PHP?

When processing and executing SQL queries from a .sql file in PHP, it is recommended to read the contents of the file, connect to the database, and execute each query individually. This ensures that each query is executed properly and prevents any errors that may occur if trying to execute the entire file at once.

<?php
// Read the contents of the .sql file
$sqlFile = file_get_contents('path/to/your/file.sql');

// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=database_name', 'username', 'password');

// Split the file contents into individual queries
$queries = explode(';', $sqlFile);

// Execute each query
foreach($queries as $query) {
    $pdo->exec($query);
}
?>