C

Simplified tutorials to learn the popular basic language C

VB.Net

Learn the basic desktop development application language

CodeIgniter

One of the popular PHP three tier (MVC) framework

How to Delete Database Data Rows


Deleting data on database is an essential operation with any application development. But we cannot delete a specific data cell on database, and the database delete operation always deletes an entire row (tuple) of data.



Here let us see how to perform delete operation with CodeIgniter.

Controller function calls a model function to delete:

public function delete()
   {
     $this->load->Model('ProjectModel');
     $this->ProjectModel->delete_user();
     echo "Deleted";
   }

Where
  • ProjectModel is the our CodeIgniter model class.
  • delete_user is the model function.
  • Echo “Deleted” is a just message to indicate the deletion.
In Model class ProjectModel, we should have a function named ‘delete_user’.

Model Class

  public function delete_user()
   {
     $this->load->database();
     $this->db->query("delete from table_user where user_age= 25");
    }
  • Where table_user is the table name and user_age is the column name on the database. Here this delete query deletes a user with age of 25.
Note:
        The query “delete from table_user…..” does not contains * symbol to represent all row data.

CodeIgniter Method of Deletion:

This method is more perfect and simple. Instead of above tradition method of deletion, you can simply customize your Model function like this:
public function delete_user()
  {
    $this->load->database();
    $this->db->where('user_age', 20);
    $this->db->delete('table_user');
   }
  • Where user_age is a column name on table ‘table_user’. 20 is the condition for user age.
Note:
    You cannot delete entire table rows using this type of delete query. But this query will delete all rows that satisfy the condition. For example: Above query delete all table rows with age 20.

Adding more conditions for deletion:

You can add more where clauses.

Example:

public function delete_user()
  {
    $this->load->database();
    $this->db->where('user_age', 20);
    $this->db->where('user_name', 'Aslam');
    $this->db->delete('table_user');
   }

This query delete a user with user_age 20 and user_name is ‘Aslam’

You can perform above operation with single line of condition:

public function delete_user()
  {
    $this->load->database();
    $this->db->delete('table_user', array('user_age' => '4', 'user_name'=>'Aslam'));
   }

Here where clause and table name are specified in a single line and conditions are arranged as an array of data.

With traditional method:

public function delete_user()
  {
    $this->load->database();
    $this->db->query("delete from table_user where user_age= 20 AND    user_name = 'Aslam' ");
  }

Using OR statements:

public function delete_user()
  {
    $this->load->database();
    $this->db->query("delete from table_user where user_age= 20 OR user_name = 'Aslam'");
  }

This query deletes all rows with user_age 20 or a user_name of ‘Aslam’. So deletion works well even one of the conditions is true.


No comments:

Post a Comment