sql
php
1.打开sql文件,放入一个变量(字符串类型)当中
2.使用正则替换掉当中的注释(“--”与“/**/”)
3.使用explode分割成为一个数组并去除每行的空格
4.链接数据库之后使用my_query()执行sql
<?php
// +------------------------------------------------------------------------------------------
// | Author: longDD <longdd_love@163.com>
// +------------------------------------------------------------------------------------------
// | There is no true,no evil,no light,there is only power.
// +------------------------------------------------------------------------------------------
// | Description: import sql Dates: 2014-08-07
// +------------------------------------------------------------------------------------------
class ImportSql
{
/** @var $content 数据库连接 */
protected $connect = null;
/** @var $db 数据库对象 */
protected $db = null;
/** @var $sqlFile sql文件 */
public $sqlFile = "";
/** @array @sqlArr sql语句数组 */
public $sqlArr = array();
/**
* 构造函数
*
* @param string $host 主机地址
* @param string $user 用户名
* @param string $pw 密码
* @param $db_name 数据库名称
* @return void
*/
public function __construct($host, $user, $pw, $db_name)
{
/** 连接数据库 */
$this->connect = mysql_connect($host, $user, $pw) or die("Could not connect: " . mysql_error());
/** 选中数据库 */
$this->db = mysql_select_db($db_name, $this->connect) or die("Yon can not select the table:" . mysql_error());
}
/**
* 导入sql文件
*
* @param string $url 文件路径
* @return true 导入成返回true
*/
public function Import($url)
{
if(!file_exists($url))
{
exit("文件不存在!");
}
$this->sqlFile = file_get_contents($url);
if (!$this->sqlFile)
{
exit("打开文件错误!");
}
else
{
$this->GetSqlArr();
if ($this->Runsql())
{
return true;
}
}
}
/**
* 获取sql语句数组
*
* @return void
*/
public function GetSqlArr()
{
/** 去除注释 */
$str = $this->sqlFile;
$str = preg_replace(‘/--.*/i‘, ‘‘, $str);
$str = preg_replace(‘/\/\*.*\*\/(\;)?/i‘, ‘‘, $str);
/** 去除空格 创建数组 */
$str = explode(";\n", $str);
foreach ($str as $v)
{
$v = trim($v);
if (empty($v))
{
continue;
}
else
{
$this->sqlArr[] = $v;
}
}
}
/**
* 执行sql文件
*
* @return true 执行成功返回true
*/
public function RunSql()
{
/** 开启事务 */
if (mysql_query(‘BEGIN‘))
{
foreach ($this->sqlArr as $k => $v)
{
if (!mysql_query($v))
{
/** 回滚 */
mysql_query(‘ROLLBACK‘);
exit("sql语句错误:第" . $k . "行" . mysql_error());
}
}
/** 提交事务 */
mysql_query(‘COMMIT‘);
return true;
}
else
{
exit(‘无法开启事务!‘);
}
}
}
// +------------------------------------------------------------------------------------------
// | End of ImportSql class
// +------------------------------------------------------------------------------------------
/**
* This is a example.
*/
header("Content-type:text/html;charset=utf-8");
$sql = new ReadSql("localhost", "root", "", "log_db");
$rst = $sql->Import("./log_db.sql");
if ($rst)
{
echo "Success!";
}
// +------------------------------------------------------------------------------------------
// | End of file ImportSql.php
// +------------------------------------------------------------------------------------------
// | Location: ./ImportSql.php
// +------------------------------------------------------------------------------------------
原文:http://blog.csdn.net/qlong_dd/article/details/38426161