Delete Query with Where Condition in Codeigniter
To delete data from a database table with a WHERE condition in CodeIgniter, you can follow these steps:
- Load the CodeIgniter database library:
$this->load->database();
This line loads the CodeIgniter database library, which allows you to interact with the database.
- Call the
delete()
method on the database object:
$this->db->delete('table_name');
Replace 'table_name'
with the name of the table from which you want to delete data.
- Add the WHERE condition to the delete query:
$this->db->where('column_name', 'value');
Replace 'column_name'
with the name of the column on which you want to apply the condition, and 'value'
with the specific value that should match the condition.
- Execute the delete query:
$this->db->delete('table_name');
This line executes the delete query and deletes the rows that match the WHERE condition.
Here's an example that combines these steps:
$this->load->database();
$this->db->delete('users');
$this->db->where('id', 1);
$this->db->delete('users');
In this example, the code deletes the row from the 'users'
table where the 'id'
column value is equal to 1
.