phpcodeigniteractiverecordsql-updatequery-builder

CodeIgniter UPDATE query with multiple SET assignments and multiple WHERE conditions


How can I convert this query to active record?

"UPDATE table_user 
 SET email = '$email', last_ip = '$last_ip' 
 where username = '$username' and status = '$status'";

I tried to convert the query above to:

$data = array('email' => $email, 'last_ip' => $ip);
$this->db->where('username',$username);
$this->db->update('table_user',$data);

How about using the where clausa status?

# must I write db->where two times like this?
$this->db->where('username', $username);
$this->db->where('status', $status);

I also tried this:

$this->db->where('username', $username, 'status', $status);

Solution

  • you can use an array and pass the array.

    Associative array method:
    $array = array('name' => $name, 'title' => $title, 'status' => $status);
    
    $this->db->where($array); 
    
    // Produces: WHERE name = 'Joe' AND title = 'boss' AND status = 'active'
    

    Or if you want to do something other than = comparison

    $array = array('name !=' => $name, 'id <' => $id, 'date >' => $date);
    
    $this->db->where($array);