php 导入sql文件到数据库
时间:2023年10月05日
/来源:网络
/编辑:佚名
php导入sql文件到数据库里,常用于安装源码
封装了一个类,摘自互联网
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/10/13
* Time: 10:25
*/
class ImportData{
//数据库信息
private $dbhost;
private $dbuser;
private $dbpw;
private $dbport;
private $dbname;
private $dbcharset;
private $link;
private $tablepre;
public function __construct($data)
{
$this->dbhost=isset($data['dbhost'])?$data['dbhost']:'';
$this->dbuser=isset($data['dbuser'])?$data['dbuser']:'';
$this->dbpw=isset($data['dbpw'])?$data['dbpw']:'';
$this->dbport=isset($data['dbport'])?$data['dbport']:'3306';
$this->dbname=isset($data['dbname'])?$data['dbname']:'';
$this->dbcharset=isset($data['dbcharset'])?$data['dbcharset']:'utf8';
$this->tablepre=isset($data['tablepre'])?$data['tablepre']:'';
$link_info=$this->link_data();
if(!$link_info['status']){
return $link_info;
}
}
//链接设置数据库
protected function link_data(){
$link=mysqli_connect($this->dbhost,$this->dbuser,$this->dbpw,null,$this->dbport);
if(!$link)
return array('status'=>false,'info'=>'数据库连接失败');
else
$this->link=$link;
//mysql 版本
//获得mysql版本
$version = mysqli_get_server_info($this->link);
//设置字符集
if($version > '4.1' && $this->dbcharset) {
mysqli_query($link, "SET NAMES {$this->dbcharset}");
}
//选择数据库
mysqli_select_db($this->link,$this->dbname);
}
//导数据
/**
* @param $dbfile 要导入的sql数据文件
* @param string $dbfile_table_pre 导入的sql文件的表前缀
* @return array
*/
public function import_data($dbfile,$dbfile_table_pre='zq_'){
if(!file_exists($dbfile)){
return array('status'=>false,'info'=>'数据库文件不存在');
}
$sql = file_get_contents($dbfile);
$status=$this->_sql_execute($this->link, $sql,$dbfile_table_pre);
if($status){
// echo '导入数据库成功';
return array('status'=>true,'info'=>'导入数据库成功');
}else{
return array('status'=>true,'info'=>'导入数据库失败');
// echo '导入数据库失败';
}
}
/**
* @param $link 数据库链接
* @param $sql 要导入的sql语句
* @param $dbfile_table_pre 导入的sql文件的表前缀
* @return bool
*/
protected function _sql_execute($link,$sql,$dbfile_table_pre) {
$sqls =$this-> _sql_split($link,$sql,$dbfile_table_pre);
if(is_array($sqls))
{
foreach($sqls as $sql)
{
if(trim($sql) != '')
{
mysqli_query($link,$sql);
}
}
}
else
{
mysqli_query($link,$sqls);
}
return true;
}
/**
* @param $link 表链接对象
* @param $sql 导入的sql
* @param $dbfile_table_pre sql文件中的sql表前缀
* @return array
*/
protected function _sql_split($link,$sql,$dbfile_table_pre) {
if(mysqli_get_server_info($link) > '4.1' && $this->dbcharset)
{
$sql = preg_replace("/TYPE=(InnoDB|MyISAM|MEMORY)( DEFAULT CHARSET=[^; ]+)?/", "ENGINE=\\1 DEFAULT CHARSET=".$this->dbcharset,$sql);
}
//如果有表前缀就替换现有的前缀
if($this->tablepre){
$sql=str_replace($dbfile_table_pre, $this->tablepre, $sql);
}
$sql = str_replace("\r", "\n", $sql);
$ret = array();
$num = 0;
$queriesarray = explode(";\n", trim($sql));
unset($sql);
foreach($queriesarray as $query)
{
$ret[$num] = '';
$queries = explode("\n", trim($query));
$queries = array_filter($queries);
foreach($queries as $query)
{
$str1 = substr($query, 0, 1);
if($str1 != '#' && $str1 != '-') $ret[$num] .= $query;
}
$num++;
}
return $ret;
}
}
/**
* 初始化数据库信息
*/
$data=array(
'dbhost'=>'127.0.0.1'
,'dbuser'=>'root'
,'dbpw'=>'root'
,'dbname'=>'phptest'
);
$obj=new ImportData($data);
$obj->import_data('./zq.sql');
封装了一个类,摘自互联网
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/10/13
* Time: 10:25
*/
class ImportData{
//数据库信息
private $dbhost;
private $dbuser;
private $dbpw;
private $dbport;
private $dbname;
private $dbcharset;
private $link;
private $tablepre;
public function __construct($data)
{
$this->dbhost=isset($data['dbhost'])?$data['dbhost']:'';
$this->dbuser=isset($data['dbuser'])?$data['dbuser']:'';
$this->dbpw=isset($data['dbpw'])?$data['dbpw']:'';
$this->dbport=isset($data['dbport'])?$data['dbport']:'3306';
$this->dbname=isset($data['dbname'])?$data['dbname']:'';
$this->dbcharset=isset($data['dbcharset'])?$data['dbcharset']:'utf8';
$this->tablepre=isset($data['tablepre'])?$data['tablepre']:'';
$link_info=$this->link_data();
if(!$link_info['status']){
return $link_info;
}
}
//链接设置数据库
protected function link_data(){
$link=mysqli_connect($this->dbhost,$this->dbuser,$this->dbpw,null,$this->dbport);
if(!$link)
return array('status'=>false,'info'=>'数据库连接失败');
else
$this->link=$link;
//mysql 版本
//获得mysql版本
$version = mysqli_get_server_info($this->link);
//设置字符集
if($version > '4.1' && $this->dbcharset) {
mysqli_query($link, "SET NAMES {$this->dbcharset}");
}
//选择数据库
mysqli_select_db($this->link,$this->dbname);
}
//导数据
/**
* @param $dbfile 要导入的sql数据文件
* @param string $dbfile_table_pre 导入的sql文件的表前缀
* @return array
*/
public function import_data($dbfile,$dbfile_table_pre='zq_'){
if(!file_exists($dbfile)){
return array('status'=>false,'info'=>'数据库文件不存在');
}
$sql = file_get_contents($dbfile);
$status=$this->_sql_execute($this->link, $sql,$dbfile_table_pre);
if($status){
// echo '导入数据库成功';
return array('status'=>true,'info'=>'导入数据库成功');
}else{
return array('status'=>true,'info'=>'导入数据库失败');
// echo '导入数据库失败';
}
}
/**
* @param $link 数据库链接
* @param $sql 要导入的sql语句
* @param $dbfile_table_pre 导入的sql文件的表前缀
* @return bool
*/
protected function _sql_execute($link,$sql,$dbfile_table_pre) {
$sqls =$this-> _sql_split($link,$sql,$dbfile_table_pre);
if(is_array($sqls))
{
foreach($sqls as $sql)
{
if(trim($sql) != '')
{
mysqli_query($link,$sql);
}
}
}
else
{
mysqli_query($link,$sqls);
}
return true;
}
/**
* @param $link 表链接对象
* @param $sql 导入的sql
* @param $dbfile_table_pre sql文件中的sql表前缀
* @return array
*/
protected function _sql_split($link,$sql,$dbfile_table_pre) {
if(mysqli_get_server_info($link) > '4.1' && $this->dbcharset)
{
$sql = preg_replace("/TYPE=(InnoDB|MyISAM|MEMORY)( DEFAULT CHARSET=[^; ]+)?/", "ENGINE=\\1 DEFAULT CHARSET=".$this->dbcharset,$sql);
}
//如果有表前缀就替换现有的前缀
if($this->tablepre){
$sql=str_replace($dbfile_table_pre, $this->tablepre, $sql);
}
$sql = str_replace("\r", "\n", $sql);
$ret = array();
$num = 0;
$queriesarray = explode(";\n", trim($sql));
unset($sql);
foreach($queriesarray as $query)
{
$ret[$num] = '';
$queries = explode("\n", trim($query));
$queries = array_filter($queries);
foreach($queries as $query)
{
$str1 = substr($query, 0, 1);
if($str1 != '#' && $str1 != '-') $ret[$num] .= $query;
}
$num++;
}
return $ret;
}
}
/**
* 初始化数据库信息
*/
$data=array(
'dbhost'=>'127.0.0.1'
,'dbuser'=>'root'
,'dbpw'=>'root'
,'dbname'=>'phptest'
);
$obj=new ImportData($data);
$obj->import_data('./zq.sql');
新闻资讯 更多
- 【建站知识】查询nginx日志状态码大于400的请求并打印整行04-03
- 【建站知识】Python中的logger和handler到底是个什么?04-03
- 【建站知识】python3拉勾网爬虫之(您操作太频繁,请稍后访问)04-03
- 【建站知识】xpath 获取meta里的keywords及description的方法04-03
- 【建站知识】python向上取整以50为界04-03
- 【建站知识】scrapy xpath遇见乱码解决04-03
- 【建站知识】scrapy爬取后中文乱码,解决word转为html 时cp1252编码问题04-03
- 【建站知识】scrapy采集—爬取中文乱码,gb2312转为utf-804-03