2012-05-10 2 views
25

어떻게이 쿼리를 활성 레코드로 변환 할 수 있습니까? 어떻게 어디 clausa 상태를 사용하는 방법에 대한multiple where 조건 codeigniter

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

:

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

나는 위의 쿼리를 변환하려고?

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

답변

57

당신이 배열을 사용하여 배열을 전달할 수 있습니다

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

나는이 시도.

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' 

아니면보다 = 비교 다른 무언가를하려는 경우

$array = array('name !=' => $name, 'id <' => $id, 'date >' => $date); 

$this->db->where($array); 
14

네, 완벽하게 유효한 방법이다()를 여러 번 호출이를 달성하기 위해.

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

http://www.codeigniter.com/user_guide/database/query_builder.html

+1

비록 이것이 이것을 달성하는 하나의 방법입니다. 키 배열을 사용하는 것이 훨씬 쉽습니다. – gorelative

+0

일부 조건을 평가 한 후 where 절을 확장해야 할 수도 있으므로 알아두면 유용합니다. – AndFisher

1

는 다목적

function ManageData($table_name='',$condition=array(),$udata=array(),$is_insert=false){ 
$resultArr = array(); 
$ci = & get_instance(); 
if($condition && count($condition)) 
    $ci->db->where($condition); 
if($is_insert) 
{ 
    $ci->db->insert($table_name,$udata); 
    return 0; 
} 
else 
{ 
    $ci->db->update($table_name,$udata); 
    return 1; 
} 

}이 기능을 시도 할 수 있습니다

$data = array(
       'email' =>$email, 
       'last_ip' => $last_ip 
      ); 

$where = array('username ' => $username , 'status ' => $status); 
$this->db->where($where); 
$this->db->update('table_user ', $data); 
+0

답장을 보내 주셔서 감사합니다., –

2

1

는이 대답을 늦게하지만 난 생각하십시오 엠 aybe 여전히 도울 수, 두 가지를 사용하여 위의 두 가지 방법을 시도합니다. 조건과 배열을 가진 메소드를 사용합니다. 그 중 어떤 것도 나에게 몇 가지 테스트를하지 않았고 조건이 실행되지 않았기 때문에 해결 방법을 시도했습니다. 코드 : 같은

$array = array('tlb_account.crid' =>$value , 'tlb_request.sign'=> 'FALSE'); 

직접 할당 :

public function validateLogin($email, $password){ 
     $password = md5($password); 
     $this->db->select("userID,email,password"); 
     $query = $this->db->get_where("users", array("email" => $email)); 
     $p = $query->row_array(); 

     if($query->num_rows() == 1 && $password == $p['password']){ 
      return $query->row(); 
     } 

    } 
1

당신처럼 모두 사용 배열을 사용할 수 있습니다

$this->db->where('tlb_account.crid' =>$value , 'tlb_request.sign'=> 'FALSE'); 

내가 당신을 도와 바랍니다.