codeigniter update query code example
Example 1: update in codeigniter query
$data = array(
'title' => $title,
'name' => $name,
'date' => $date
);
$this->db->where('id', $id);
$this->db->update('mytable', $data);
Example 2: update query in codeigniter using where condition
public function update_row(){
$update_rows = array('field-name' => 'field-data',);
$this->db->where('id', 1 );
$this->db->update('table-name', $update_rows);
}
Example 3: codeigniter where_not_in
$names = array('Frank', 'Todd', 'James');
$this->db->where_not_in('username', $names);
Example 4: codeigniter where order by
$this->db->select('ae_users.employee_id, ae_users.emp_name, ae_users.emp_name2, ae_users.emp_name3');
$this->db->from('ae_users');
$this->db->where_in('ae_users.employee_id',$employee_ids);
$this->db->_protect_identifiers = FALSE;
$order = sprintf('FIELD(ae_users.employee_id, %s)', implode(', ', $employee_ids));
$this->db->order_by($order);
$this->db->_protect_identifiers = TRUE;
$query =$this->db->get();
if ($query->num_rows()) {
return $query->result_array();
} else {
return 0;
}
Example 5: codeigniter update query return value
public function update_row(){
$update_rows = array(
'name' => 'rincky',
'address' => 'India',
'contact' => '98545785',
'department' => 'IT',
);
$this->db->where('id', 1 );
$result = $this->db->update('user', $update_rows);
return $result;
}
Example 6: codeigniter insert
$data = array(
'title' => 'My title',
'name' => 'My Name',
'date' => 'My date'
);
$this->db->insert('mytable', $data);