What are some alternative methods or libraries that can be used to import an SQL file into a database with PHP, aside from phpMyAdmin?

When needing to import an SQL file into a database with PHP, an alternative method to phpMyAdmin is to use the MySQLi extension or PDO (PHP Data Objects) in PHP. These libraries allow for database connections and queries to be executed directly within the PHP code, providing more flexibility and control over the import process.

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Read SQL file
$sql = file_get_contents('path/to/your/sql/file.sql');

// Execute multi query
if ($conn->multi_query($sql) === TRUE) {
    echo "SQL file imported successfully";
} else {
    echo "Error importing SQL file: " . $conn->error;
}

// Close connection
$conn->close();
?>