如何备份数据库并截断 CodeIgniter 中的所有表?
How to back db and truncate all tables in CodeIgniter?
我正在使用此代码备份数据,如何截断数据库中的所有表?
我的 backupdb 函数
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);
}
如何在上面的函数中添加截断代码?
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);
我正在使用此代码备份数据,如何截断数据库中的所有表?
我的 backupdb 函数
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);
}
如何在上面的函数中添加截断代码?
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);