Read the contents of the file
$_sql = file_get_contents (' Test.sql ');
$_arr = explode ('; ', $_sql);
$_mysqli = new mysqli (db_host,db_user,db_pass);
if (Mysqli_connect_errno ()) {
exit (' Connection database error ');
}
Execute SQL statement
foreach ($_arr as $_value) {
$_mysqli->query ($_value. '; ');
}
$_mysqli->close ();
$_mysqli = null;
Above text.sql is the SQL file you need to execute, db_host host name, Db_user username, db_pass password!
This is just the most basic automatic execution of SQL files, you can also customize the name of the build database, by deleting the following code in the SQL file
CREATE db IF not EXISTS database name DEFAULT CHARACTER SET UTF8 COLLATE utf8_general_ci;
Use database name
Then add the code before executing all the SQL statements in text.php
$_mysqli->query ("CREATE database IF not EXISTS name DEFAULT CHARACTER SET UTF8 COLLATE utf8_general_ci;");
$_mysqli->query ("Use database name");
The above is the entire content of this article, I hope to help you.