update codeigniter 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);
// Produces:
//
//      UPDATE mytable
//      SET title = '{$title}', name = '{$name}', date = '{$date}'
//      WHERE id = $id

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 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 4: where group codeigniter

$this->db->select('*')->from('my_table')
        ->group_start()
                ->where('a', 'a')
                ->or_group_start()
                        ->where('b', 'b')
                        ->where('c', 'c')
                ->group_end()
        ->group_end()
        ->where('d', 'd')
->get();

// Generates:
// SELECT * FROM (`my_table`) WHERE ( `a` = 'a' OR ( `b` = 'b' AND `c` = 'c' ) ) AND `d` = 'd'

Example 5: codeigniter select for update

$table = "my_table";
$id = 1;
$update = ["status"=>"working"];
//Edit just above /\ if you don't need extra "where" clause
$query = $this->db->select()
            ->from($table)
            ->where('id', $id)
            ->get_compiled_select();
$data = $this->db->query("$query FOR UPDATE")->row_array();
$this->db->where('id', $data['id'])->update($table,$update);

Tags:

Misc Example