PHP Import SQL file
PHP Import SQL file
sql
php
PHP Import SQL file
Basic ideas
1. Open the SQL file and put it into a variable (string type)
2. Use regular to replace comments ("--" and "/**/")
3. Use explode to split into an array and strip each line of space
4. Using My_query () to execute SQL after linking the database
Code
// +------------------------------------------------------------------------------------------
// | Author: longDD
// +------------------------------------------------------------------------------------------
// | 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://www.bkjia.com/phpjc/871202.html www.bkjia.com true http://www.bkjia.com/phpjc/871202.html Techarticle php Import SQL file PHP import SQL file sql PHP PHP Import SQL file basic idea 1. Open the SQL file and put it into a variable (string type) 2. Use regular to replace comments in ...
-