如何在codeigniter中运行mysql转储文件

时间:2013-12-28 21:33:30

标签: php mysql codeigniter

$folder=FCPATH."application\\models\\new.sql";
$filez=fopen($folder,'w');
$r2=str_replace("cms_project_db","new_cms",$filez);

$new=fwrite($filez,$r2);

$content=file_get_contents($new);
$arr=explode(";",$content);
foreach($arr as $value=>$val)
{
    $this->db->query($val);
}       

我想从SQL转储文件获取所有内容并更改数据库名称然后执行所有查询,但是我收到此错误:

  

发生数据库错误

1 个答案:

答案 0 :(得分:0)

您可以使用以下脚本,希望它能为您提供帮助:

$this->load->dbutil();

        $prefs = array(     
                'format'      => 'zip',             
                'filename'    => 'my_backup.sql'
              );


        $backup =& $this->dbutil->backup($prefs); 

        $db_name = 'backup-'. date("Y-m-d-H-i-s") .'.zip';
        $save = 'path_to_file/'.$db_name;

        $this->load->helper('file');
        write_file($save, $backup); 


        $this->load->helper('download');
        force_download($db_name, $backup);