Codeigniter Database Where and Delete Beginners Tutorial Part-4
In previous tutorial we learned about the update(), replace() and set methods of codeigniter database, in this tutorial we will learn about the delete method and available where methods of codeigniter. So let us start.
Codeigniter delete() tutorial
Deleting Data With Codeigniter “$this->db->delete()”
The codeigniter delete() method allows us to let remove a row from out table, this works as same as the MySql delete method, suppose you have users in your database table and you want to remove one user from that table then the delete method of codeigniter will let you do so. Let us see an example.
Suppose we have a user table and we want to remove the user with id 4 from the table then we will use the following method to remove that user using codeigniter delete.
Note: On the above query we are passing the id and the table name to the codeigniter delete() parameter. This will generate the following sql query
DELETE FROM user WHERE id=$id
Output:delete() model:before delete():after delete():
It is also possible to use codeigniter where methods with the codeigniter delete() method, next example elaborates how to use where methods with codeigniter delete().
Using Codeigniter where() With delete()
The codeigniter delete() method itself doesn’t give that much flexibility, if I want to delete a row with multiple identifiers then I can’t use the delete() method because of its limitation of single identifier, so here comes the where() method, it allows us to use multiple identifiers, the where() method can be used with delete method, below given example explains it’s usage:
Note: in the above codeigniter query request, the where clause will set the identifier, you can use multiple where() methods and codeigniter will generate the query.
Output:delete() model:Before where() delete():After where() delete()
Codeigniter where() Methods Tutorial
Codeigniter Where “$this->db->where()” method
The codeigniter where() method allows to select a specific data from table, the where() clause if repeated then it will generate a SQL query with “AND” parameters and append the query. There are 4 ways in codeigniter of using the where clause.
Simple Key and Value Method
In simple key value method we pass two values to our where clauses, the key and the value and append with multiple where clause to get a complete query, see the example below.
This will generate the string: WHERE key=value
If we repeat the where clause then:
The codeigniter or_where_not_in() method allows us let prepend our SQL “IN” request with NOT. See example below
The above codeigniter code will generate the following SQL query:
WHERE `id`='4' OR `username` NOT IN ('robin', 'taylor')
That's all for this tutorial folks, in next tutorial we will start up with codeigniter forms, and learn how we can create and process forms using codeigniter. If you have any suggestions or questions please do comment below. If you found this tutorial helpful then share with your friends.