php导入sql文件_PHP教程

WBOY
发布: 2016-07-13 10:19:49
原创
994 人浏览过

php导入sql文件

php导入sql文件

sql php


php导入sql文件

基本思路

1.打开sql文件,放入一个变量(字符串类型)当中

2.使用正则替换掉当中的注释(“--”与“/**/”)

3.使用explode分割成为一个数组并去除每行的空格

4.链接数据库之后使用my_query()执行sql

代码

<ol class="linenums"><li class="L0"><code class="language-php"><?php</code><li class="L1"><code class="language-php"></code><li class="L2"><code class="language-php">// &#43;------------------------------------------------------------------------------------------</code><li class="L3"><code class="language-php">// | Author: longDD <longdd_love@163.com></code><li class="L4"><code class="language-php">// &#43;------------------------------------------------------------------------------------------</code><li class="L5"><code class="language-php">// | There is no true,no evil,no light,there is only power.</code><li class="L6"><code class="language-php">// &#43;------------------------------------------------------------------------------------------</code><li class="L7"><code class="language-php">// | Description: import sql Dates: 2014-08-07</code><li class="L8"><code class="language-php">// &#43;------------------------------------------------------------------------------------------</code><li class="L9"><code class="language-php"></code><li class="L0"><code class="language-php"></code><li class="L1"><code class="language-php">class ImportSql </code><li class="L2"><code class="language-php">{</code><li class="L3"><code class="language-php"></code><li class="L4"><code class="language-php">    /** @var $content 数据库连接 */</code><li class="L5"><code class="language-php">    protected $connect = null;</code><li class="L6"><code class="language-php">    /** @var $db 数据库对象 */</code><li class="L7"><code class="language-php">    protected $db = null;</code><li class="L8"><code class="language-php">    /** @var $sqlFile sql文件 */</code><li class="L9"><code class="language-php">    public $sqlFile = "";</code><li class="L0"><code class="language-php">    /** @array @sqlArr sql语句数组 */</code><li class="L1"><code class="language-php">    public $sqlArr = array();</code><li class="L2"><code class="language-php"></code><li class="L3"><code class="language-php">    /** </code><li class="L4"><code class="language-php">     * 构造函数</code><li class="L5"><code class="language-php">     * </code><li class="L6"><code class="language-php">     * @param string $host 主机地址</code><li class="L7"><code class="language-php">     * @param string $user 用户名</code><li class="L8"><code class="language-php">     * @param string $pw 密码</code><li class="L9"><code class="language-php">     * @param $db_name 数据库名称</code><li class="L0"><code class="language-php">     * @return void</code><li class="L1"><code class="language-php">     */</code><li class="L2"><code class="language-php">    public function __construct($host, $user, $pw, $db_name) </code><li class="L3"><code class="language-php">    {</code><li class="L4"><code class="language-php">        /** 连接数据库 */</code><li class="L5"><code class="language-php">        $this->connect = mysql_connect($host, $user, $pw) or die("Could not connect: " . mysql_error());</code><li class="L6"><code class="language-php">        /** 选中数据库 */</code><li class="L7"><code class="language-php">        $this->db = mysql_select_db($db_name, $this->connect) or die("Yon can not select the table:" . mysql_error());</code><li class="L8"><code class="language-php">    }</code><li class="L9"><code class="language-php"></code><li class="L0"><code class="language-php">    /** </code><li class="L1"><code class="language-php">     * 导入sql文件</code><li class="L2"><code class="language-php">     * </code><li class="L3"><code class="language-php">     * @param string $url 文件路径</code><li class="L4"><code class="language-php">     * @return true 导入成返回true</code><li class="L5"><code class="language-php">     */</code><li class="L6"><code class="language-php">    public function Import($url) </code><li class="L7"><code class="language-php">    {</code><li class="L8"><code class="language-php">        if(!file_exists($url))</code><li class="L9"><code class="language-php">        {</code><li class="L0"><code class="language-php">            exit("文件不存在!");</code><li class="L1"><code class="language-php">        }</code><li class="L2"><code class="language-php"></code><li class="L3"><code class="language-php">        $this->sqlFile = file_get_contents($url);</code><li class="L4"><code class="language-php"></code><li class="L5"><code class="language-php">        if (!$this->sqlFile) </code><li class="L6"><code class="language-php">        {</code><li class="L7"><code class="language-php">            exit("打开文件错误!");</code><li class="L8"><code class="language-php">        } </code><li class="L9"><code class="language-php">        else </code><li class="L0"><code class="language-php">        {</code><li class="L1"><code class="language-php">            $this->GetSqlArr();</code><li class="L2"><code class="language-php"></code><li class="L3"><code class="language-php">            if ($this->Runsql()) </code><li class="L4"><code class="language-php">            {</code><li class="L5"><code class="language-php">                return true;</code><li class="L6"><code class="language-php">            }</code><li class="L7"><code class="language-php">        }</code><li class="L8"><code class="language-php">    }</code><li class="L9"><code class="language-php"></code><li class="L0"><code class="language-php">    /**</code><li class="L1"><code class="language-php">     * 获取sql语句数组</code><li class="L2"><code class="language-php">     * </code><li class="L3"><code class="language-php">     * @return void</code><li class="L4"><code class="language-php">     */</code><li class="L5"><code class="language-php">    public function GetSqlArr() </code><li class="L6"><code class="language-php">    {</code><li class="L7"><code class="language-php">        /** 去除注释 */</code><li class="L8"><code class="language-php">        $str = $this->sqlFile;</code><li class="L9"><code class="language-php">        $str = preg_replace('/--.*/i', '', $str);</code><li class="L0"><code class="language-php">        $str = preg_replace('/\/\*.*\*\/(\;)?/i', '', $str);</code><li class="L1"><code class="language-php"></code><li class="L2"><code class="language-php">        /** 去除空&#26684; 创建数组 */</code><li class="L3"><code class="language-php">        $str = explode(";\n", $str);</code><li class="L4"><code class="language-php"></code><li class="L5"><code class="language-php">        foreach ($str as $v) </code><li class="L6"><code class="language-php">        {</code><li class="L7"><code class="language-php">            $v = trim($v);</code><li class="L8"><code class="language-php"></code><li class="L9"><code class="language-php">            if (empty($v)) </code><li class="L0"><code class="language-php">            {</code><li class="L1"><code class="language-php">                continue;</code><li class="L2"><code class="language-php">            } </code><li class="L3"><code class="language-php">            else </code><li class="L4"><code class="language-php">            {</code><li class="L5"><code class="language-php">                $this->sqlArr[] = $v;</code><li class="L6"><code class="language-php">            }</code><li class="L7"><code class="language-php">        }</code><li class="L8"><code class="language-php">    }</code><li class="L9"><code class="language-php"></code><li class="L0"><code class="language-php">    /**</code><li class="L1"><code class="language-php">     * 执行sql文件</code><li class="L2"><code class="language-php">     * </code><li class="L3"><code class="language-php">     * @return true 执行成功返回true</code><li class="L4"><code class="language-php">     */</code><li class="L5"><code class="language-php">    public function RunSql() </code><li class="L6"><code class="language-php">    {</code><li class="L7"><code class="language-php">        /** 开启事务 */</code><li class="L8"><code class="language-php">        if (mysql_query('BEGIN'))</code><li class="L9"><code class="language-php">        {</code><li class="L0"><code class="language-php">            foreach ($this->sqlArr as $k => $v)</code><li class="L1"><code class="language-php">            {</code><li class="L2"><code class="language-php">                if (!mysql_query($v)) </code><li class="L3"><code class="language-php">                {</code><li class="L4"><code class="language-php">                    /** 回滚 */</code><li class="L5"><code class="language-php">                    mysql_query('ROLLBACK');</code><li class="L6"><code class="language-php"></code><li class="L7"><code class="language-php">                    exit("sql语句错误:第" . $k . "行" . mysql_error());</code><li class="L8"><code class="language-php">                }</code><li class="L9"><code class="language-php">            }</code><li class="L0"><code class="language-php"></code><li class="L1"><code class="language-php">            /** 提交事务 */</code><li class="L2"><code class="language-php">            mysql_query('COMMIT');</code><li class="L3"><code class="language-php">            return true;</code><li class="L4"><code class="language-php">        }</code><li class="L5"><code class="language-php">        else</code><li class="L6"><code class="language-php">        {</code><li class="L7"><code class="language-php">            exit('无法开启事务!');</code><li class="L8"><code class="language-php">        }</code><li class="L9"><code class="language-php">    }</code><li class="L0"><code class="language-php"></code><li class="L1"><code class="language-php">}</code><li class="L2"><code class="language-php"></code><li class="L3"><code class="language-php">// &#43;------------------------------------------------------------------------------------------</code><li class="L4"><code class="language-php">// | End of ImportSql class</code><li class="L5"><code class="language-php">// &#43;------------------------------------------------------------------------------------------</code><li class="L6"><code class="language-php"></code><li class="L7"><code class="language-php"></code><li class="L8"><code class="language-php">/**</code><li class="L9"><code class="language-php">* This is a example.</code><li class="L0"><code class="language-php">*/</code><li class="L1"><code class="language-php"></code><li class="L2"><code class="language-php">header("Content-type:text/html;charset=utf-8");</code><li class="L3"><code class="language-php"></code><li class="L4"><code class="language-php">$sql = new ReadSql("localhost", "root", "", "log_db");</code><li class="L5"><code class="language-php"></code><li class="L6"><code class="language-php">$rst = $sql->Import("./log_db.sql");</code><li class="L7"><code class="language-php"></code><li class="L8"><code class="language-php">if ($rst) </code><li class="L9"><code class="language-php">{</code><li class="L0"><code class="language-php">   echo "Success!";</code><li class="L1"><code class="language-php">}</code><li class="L2"><code class="language-php"></code><li class="L3"><code class="language-php">// &#43;------------------------------------------------------------------------------------------</code><li class="L4"><code class="language-php">// | End of file ImportSql.php</code><li class="L5"><code class="language-php">// &#43;------------------------------------------------------------------------------------------</code><li class="L6"><code class="language-php">// | Location: ./ImportSql.php</code><li class="L7"><code class="language-php">// &#43;------------------------------------------------------------------------------------------</code>
登录后复制
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板