ekliu 发表于 2010-11-23 21:50:49

CI数据库备份与还原入门小例子 求助!

本帖最后由 ekliu 于 2010-11-24 11:06 编辑

按照jeongee的帖子
http://codeigniter.org.cn/forums/thread-6626-1-1.html
DB导出正常
但是在还原的的时候
SQL 报错:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'source database.sql' at line 1
路径是正确的
都已经放在PHP文件的同级下了(C层),
应该是SQL语句写错了,但是就是不知道应该怎么写,
我的写法   
   $file = "database.sql";
      var_dump($file);
      //$bool = mysql_query("source database.sql")or die(mysql_error());   
          $bool = mysql_query("source'".$file."';")or die(mysql_error());
          $bool = mysql_query("LOAD DATA INFILE $file") or die(mysql_error());
    都不好用

jeongee 发表于 2010-11-23 23:24:02

本帖最后由 jeongee 于 2010-11-23 23:25 编辑

路径错了,放在与CI入口文件同级的目录下或者使用绝对路径

ekliu 发表于 2010-11-24 00:20:02

无论是index.php 同级下
还是system文件夹下
绝对路径:
string(41) "C:/xampp/htdocs/mbmg2/system/database.sql" You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'source 'C:/xampp/htdocs/mbmg2/system/database.sql'' at line 1
都是一样。。。。。。。。。

ekliu 发表于 2010-11-24 09:15:42

$this->load->helper('file');
         
      $content = read_file('./database.sql');
      var_dump($content);//能访问到
      $conn=mysql_connect("localhost","root","123456");//指定数据库连接参数
      mysql_select_db("mbmg2");
         
      //$file = 'D:\xampp\htdocs\mbmg2\database.sql';
      //$file = './database.sql';
      $file = 'http://localhost/mbmg2/database.sql';
      $bool = mysql_query("source '".$file."'") or die(mysql_error());
      
      var_dump($bool);
      
      mysql_close($conn);

以上3中写法都不好用,
晕了。一定是一个小问题,就找不到了。。
jeongee

jeongee 发表于 2010-11-24 09:29:45

这,要不然先去掉注释再搞下

$content = read_file('./database.sql');
$content = preg_replace("/#(.*)\s#(.*)TABLE(.*)(.*)\s#/i","",$content);//去掉注释部分

ekliu 发表于 2010-11-24 10:37:03

用你的第一方法好用 我修改一下
      $this->load->helper('file');
         
      $content = read_file('./database.sql');
         
      $content = preg_replace("/#(.*)\s#(.*)TABLE(.*)(.*)\s#/i","",$content);//去掉注释部分
         
      $sqls = explode(";",$content);
      
      foreach($sqls as $key=>$sql)
      {   
            $sql = trim($sql);

            if(!empty($sql))
            {
                $bool = $this->db->query($sql) or die(mysql_error());
            }
         
      }

谢谢jeongee
页: [1]
查看完整版本: CI数据库备份与还原入门小例子 求助!