peter
peter

Reputation: 95

Codeigniter 3: delete all rows older than 2 months

I'm using codeigniter 3. I want to delete all the rows from table "anwser" that are older than 2 months (datetime in mysql tabel). I don't get an error. But no rows are deleted. Any suggestions?

model:

    function delete_old_anwsers()
{
    $this->load->model('quiz_model');
    $this->db->where('datum <', 'strtotime('-2 month')');
    $this->db->delete('anwser');
}

controller:

$this->quiz_model->delete_old_anwsers();

Thank you

Solution:

    function delete_old_answer()
{
    $this->db->query("DELETE FROM answer WHERE datum < NOW() - INTERVAL 2 MONTH ");
}

Upvotes: 3

Views: 2323

Answers (2)

Michal Škoula
Michal Škoula

Reputation: 1

In Active Record it will be:

$this->db->where('datum < DATE_SUB(NOW(), INTERVAL 60 DAY)');
$this->db->delete('anwser');

Upvotes: 0

Naveed Ramzan
Naveed Ramzan

Reputation: 3593

You need to write a crone job which will be executed daily.

In that, you can add this query and it will delete all answers which are old than 2 months.

Upvotes: 1

Related Questions