Reputation: 47
I have sql query for delete multiple record from database
$sql="delete from users where id IN(4,5,6..)";
How to run this query in codeigniter
Upvotes: 1
Views: 3697
Reputation: 2885
Per the documents, you can use the query builder, or you can run a direct query on the database.
The query() function returns a database result object when “read” type queries are run, which you can use to show your results. When “write” type queries are run it simply returns TRUE or FALSE depending on success or failure. When retrieving data you will typically assign the query to your own variable, like this:
$query = $this->db->query('delete from users where id IN(4,5,6..)');
Or you can use the query builder class which allows you to do the following:
$this->db->where_in('id', array(4,5,6));
$this->db->delete('users');
Upvotes: 5
Reputation: 114
You can also execute query like this
$delete = "id IN('4','5','6')";
$this->db->where($delete);
$this->db->delete("user");
In this you can also execure your core query.
Upvotes: 1
Reputation: 5501
Create a function in you model
function rowDelete($ids)
{
$this->db->where_in('id', $ids);
$this->db->delete('testimonials');
}
And call that in your controller
$this->your_model->rowDelete($ids) // $ids = array(4,5,6);
Upvotes: 1