How can PHP be used to execute SQL commands from a large SQL file without using PHPMYADMIN?
To execute SQL commands from a large SQL file without using PHPMYADMIN, you can use PHP to read the SQL file line by line and execute each SQL command individually using a database connection. This allows you to automate the process of running SQL commands without the need for manual intervention.
<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Read the SQL file
$sqlFile = 'path/to/your/sqlfile.sql';
$sqlCommands = file_get_contents($sqlFile);
$sqlCommands = explode(';', $sqlCommands);
// Execute each SQL command
foreach ($sqlCommands as $sqlCommand) {
if (trim($sqlCommand) != '') {
$result = $conn->query($sqlCommand);
if (!$result) {
echo "Error executing SQL command: " . $conn->error;
}
}
}
// Close the database connection
$conn->close();
?>