简体   繁体   中英

how can i run mysql dump file in codeignniter

$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);
}       

I want to get all content from SQL dump file and change database name then execute all queries, but I get this error:

A Database Error Occurred

You can use script bellow, hope it will help you:

$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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM