Codeigniter Delete Query – how to run Delete Query in CodeIgniter
In this post we will show you how to execute CodeIgniter Delete Query with delete(), empty_table(), truncate() and Delete With Join.
CodeIgniter “Delete” query execute by following functions
- $this->db->delete()
- $this->db->empty_table()
- $this->db->truncate()
- Delete With Join
Codeigniter Delete Query with $this->db->delete()
In this functions of delete()
we can delete data of user table with passing id. if we have to delete multiple table with same id, then it will very useful way to execute query.
// pass id $id = 17; $this->db->delete('tb_alll_user', array('id' => $id)); // sql query for DELETE // DELETE FROM tb_alll_user WHERE id = $id
// pass id $id = 17; // check where id is 17 $this->db->where('id', $id); $this->db->delete('tb_alll_user'); // sql query for DELETE //DELETE FROM tb_alll_user WHERE id = $id
bu An array of names of table can passed into delete()
, if we would like to delete data from more than 1 table and single execution.
// pass id $id = 17; $tables = array('user_all_table1', 'user_all_table2', 'user_all_table3'); // check where id is 17 $this->db->where('id', $id); $this->db->delete($tables); // sql query for DELETE // DELETE FROM user_all_table1 WHERE id = $id // DELETE FROM user_all_table2 WHERE id = $id // DELETE FROM user_all_table3 WHERE id = $id
Codeigniter Delete Query with $this->db->empty_table()
by using empty_table()
we can DELETE all data of table.
$this->db->empty_table('tb_alll_user'); // sql query for DELETE // DELETE FROM tb_alll_user
Codeigniter Delete Query with $this->db->truncate()
by using truncate()
we can truncate table.
$this->db->from('tb_alll_user'); $this->db->truncate(); // (OR) $this->db->truncate('tb_alll_user'); // sql quey like TRUNCATE table data // TRUNCATE table tb_alll_user;
Codeigniter Delete Query with Delete With Join
by using “Delete With Join” we can delete data table like select join query in delete.
// pass id $id = 17; $this->db->from("user_all_table1"); $this->db->join("user_all_table2", "user_all_table1.t1_id = user_all_table2.t2_id"); // check where id is 17 $this->db->where("user_all_table2.t2_id", $id); $this->db->delete("user_all_table1");
You also like codeigniter select query and codeigniter join tables