Home > Article > Backend Development > Import sql file in php_PHP tutorial
sql
php
1. Open the sql file and put it into a variable (string type)
2. Use regular expressions to replace the comments ("--" and "/**/")
3. Use explode to split into an array and remove spaces from each line
4. After connecting to the database, use my_query() to execute 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">// +------------------------------------------------------------------------------------------</code><li class="L3"><code class="language-php">// | Author: longDD <longdd_love@163.com></code><li class="L4"><code class="language-php">// +------------------------------------------------------------------------------------------</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">// +------------------------------------------------------------------------------------------</code><li class="L7"><code class="language-php">// | Description: import sql Dates: 2014-08-07</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">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"> /** 去除空格 创建数组 */</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">// +------------------------------------------------------------------------------------------</code><li class="L4"><code class="language-php">// | End of ImportSql class</code><li class="L5"><code class="language-php">// +------------------------------------------------------------------------------------------</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">// +------------------------------------------------------------------------------------------</code><li class="L4"><code class="language-php">// | End of file ImportSql.php</code><li class="L5"><code class="language-php">// +------------------------------------------------------------------------------------------</code><li class="L6"><code class="language-php">// | Location: ./ImportSql.php</code><li class="L7"><code class="language-php">// +------------------------------------------------------------------------------------------</code>