>  기사  >  백엔드 개발  >  php导入sql文件_PHP教程

php导入sql文件_PHP教程

WBOY
WBOY원래의
2016-07-13 10:19:491006검색

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>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.