CodeIgniter Delete Query Example: This tutorial will show you how to delete data from database in codeigniter. Delete operation is one among the primitive database process called CRUD. As the name suggests DELETE process is used to delete records/rows from database. Similar to database Insert and Update - Delete can be performed upon single record, multiple records, upon entire table or on multiple tables at once.
CodeIgniter Delete Query Example:
CodeIgniter Active Records Class provides the function
$this->db->delete(); to write database independent delete queries which deletes the record(s) from database.
This method produces delete query string with the parameters provided and runs the query. The first parameter is the table name and the second one is the where clause condition.
MySQL Table - Employees
Let's take this mysql table as an example and see how to write different types of delete queries in codeigniter.
How to Write Delete Query in CodeIgniter?
Below codeigniter delete query with where condition will delete record(s) from 'Employees' table which has the value 'Herrod Chandler' for 'EmpName' field.
$this->db->where('EmpName', 'Herrod Chandler'); $this->db->delete('Employees'); // Produces Delete Query: // DELETE FROM Employees WHERE EmpName = 'Herrod Chandler';
There is an alternative way to write delete query in codeigniter. You can either use separate where clause like above or pass it as a string to $this->db->delete() function just like codeigniter update query.
$this->db->delete('Employees', array('EmpName' => 'Herrod Chandler')); // Produces Delete String: // DELETE FROM Employees WHERE EmpName = 'Herrod Chandler';
Delete Multiple Records in Codeigniter:
You can delete multiple records at once in codeigniter. This below example will delete the employee records whose 'EmpID' value matches in the list provided.
$id = array(2, 4, 6); $this->db->where_in('EmpID', $id); $this->db->delete('Employees'); // Produces Delete SQL: // DELETE FROM Employees WHERE EmpID IN (2, 4, 6);
Delete All Rows from Table in CodeIgniter:
If you want to delete all rows/records from a table, you should use
$this->db->empty_table('Employees'); // Produces Query: // DELETE FROM Employees;
Using $this->db->truncate() Method:
This query generates truncate SQL string and executes it.
$this->db->truncate('Employees'); // Produces SQL: // TRUNCATE Employees;
Delete Multiple Tables in Codeigniter:
Wonder if you can delete records from multiple tables at once? Yep! You can. While using multiple
delete() make sure to pass the where_clause as an array to the function. This is the perfect example for deleting rows from multiple tables at once in codeignitar.
$this->db->delete('Employees', array('DeptName' => 'HQ')); $this->db->delete('Department', array('DepartmentName' => 'HQ')); // Produces Delete Query: // DELETE FROM Employees WHERE DeptName = 'HQ'; // DELETE FROM Department WHERE DepartmentName = 'HQ';
Related Read: Delete Data from Database using Codeigniter & MySQL
That explains all about codeigniter delete query example. Using the above examples you can easily delete data from database using codeigniter and mysql.