//读取文件内容 $_sql = file_get_contents('test.sql'); $_arr = explode(';', $_sql); $_mysqli = new mysqli(DB_HOST,DB_USER,DB_PASS); if (mysqli_connect_errno()) { exit('连接数据库出错'); } //执行sql语句 foreach ($_arr as $_value) { $_mysqli->query($_value.';'); } $_mysqli->close(); $_mysqli = null;
The above text.sql is the sql file you need to execute, DB_HOST host name, DB_USER user name, DB_PASS password!
This is just the most basic automatically executed sql file. You can also customize the name of the generated database by deleting the following code in the sql file
CREATE DATABASE IF NOT EXISTS 数据库名 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE database name
Then add code before executing all sql statements in text.php
$_mysqli->query("CREATE DATABASE IF NOT EXISTS 数据库名 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;"); $_mysqli->query("USE 数据库名");
The above is the entire content of this article, I hope it will be helpful to everyone.