1

我正在使用此代码备份数据,如何截断数据库中的所有表?

我的备份数据库功能

public function export_db() {
        $this->load->dbutil();
        $prefs = array(
            'format' => 'zip',
            'filename' => date("Y-m-d-H-i-s").'.sql'
        );
        $backup = & $this->dbutil->backup($prefs);
        $db_name = 'db-' . date("Y-m-d-H-i-s") . '.zip';
        $save = 'assets/database_backup/' . $db_name;
        $this->load->helper('file');
        write_file($backup);
        $this->load->helper('download');
        force_download($db_name, $backup);
    }

如何在上述函数中添加截断代码?

4

1 回答 1

3
public function db_backup()
{
    $this->load->helper('url');
    $this->load->helper('file');
    $this->load->helper('download');
    $this->load->library('zip');
    $this->load->dbutil();
    $db_format=array(
        'format'=>'zip',
        'filename'=>'my_db_backup.sql'
    );
    $backup=& $this->dbutil->backup($db_format);
    $dbname='backup-on-'.date('Y-m-d').'.zip';
    $save='assets/db_backup/'.$dbname;
    write_file($save,$backup);
    force_download($dbname,$backup);

    $this->db->truncate('table_name');  //truncate the table.
    
}

注意:-有关此的更多信息

https://codeigniter.com/userguide3/database/query_builder.html

解决方案一

$this->db->truncate('table_name1');
$this->db->truncate('table_name2');
$this->db->truncate('table_name3');

解决方案二

 function emptytablesbycomma($stringoftables) {
     $array_tablenames = explode(",", $stringoftables);
     if (!empty($array_tablenames)) {
        foreach ($array_tablenames as $tablename) {
            $this->db->truncate($tablename);
        }
     }
 }

$stringoftables='table_name1,table_name2,table_name3';
$this->emptytablesbycomma($stringoftables);
于 2020-11-09T07:18:12.130 回答