How do I clear a mysql table?
Hello everyone
How can I clear the table of all records with a single query?
Now I have this code on CI:
File model.php
function clear_log() {
$this->db->where('event' > '0')->delete("login_history");
}
File controler.php
function clear_log()
{
$this->logs_model->clear_log();
$this->session->set_flashdata('message', lang('admin currency msg save_success'));
redirect(site_url("admin/logs"));
}
File view.php
<a href="<?php echo site_url("admin/logs/clear_log/") ?>" class="btn btn-danger">очистить</a>
When executing, I don't get any errors, however, the DB entries are not deleted. Thank you for any help!
0
Author: Anton Bogomolov, 2017-04-29
2 answers
To clear the table, use truncate()
:
$this->db->truncate('login_history');
Unlike empty_table()
, which generates DELETE FROM
SQL query, truncate()
generates TRUNCATE TABLE
, which is more productive because it deletes data by deallocating those pages that store tabular data and only these deallocations are recorded in the transaction log. DELETE FROM
in turn deletes the rows in the table and leaves an entry in the transaction log for each one.
2
Author: MihailPw, 2017-04-30 06:59:05
$this->db->empty_table('mytable'); // Produces: DELETE FROM mytable
2
Author: StAlKeRxXl, 2017-04-29 10:26:27