Home > Backend Development > PHP Tutorial > PHP executes SQL files and imports SQL files into the database, _PHP Tutorial

PHP executes SQL files and imports SQL files into the database, _PHP Tutorial

WBOY
Release: 2016-07-12 09:08:18
Original
851 people have browsed it

PHP executes the SQL file and imports the SQL file into the database,


//读取文件内容
$_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;
Copy after login

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;
Copy after login

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 数据库名");
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1057091.htmlTechArticlePHP executes the SQL file and imports the SQL file into the database, //Read the file content $_sql = file_get_contents(' test.sql');$_arr = explode(';', $_sql);$_mysqli = new mysqli(DB_HOST,DB_USER,D...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template