2017-04-06 1 views
1

Codeigniter 3, PHP 및 MySQL을 사용하고 있습니다.Codeigniter Select (조건부) 업데이트 쿼리

MySQL 데이터베이스에서 레코드를 선택하려고하는데 결과에 따라 업데이트 쿼리가 실행됩니다.

If result = 0, update. 
If result = 1, do nothing. 

내 코드는 지금까지입니다.

public function addData($itemId) { 
    $gDescription = 'test'; 
    $gPreviewLink = 'test'; 
    $gThumbnail = 'test'; 
    $gPageCount = 'test'; 

    $this->db->select('g_data'); 
    $this->db->from('item'); 
    $this->db->where('item_id', $itemId); 
    $query = $this->db->get(); 
    $result = $query->result(); 
    // var_dump($result) returns array(1) { [0]=> object(stdClass)#22 (1) { ["g_data"]=> string(1) "0" } } 

     $data = array(
     'item_description' => $gDescription, 
     'item_preview_link' => $gPreviewLink, 
     'item_thumbnail' => $gThumbnail, 
     'item_pageCount' => $gPageCount, 
     'g_data'   => '1', 
     'g_data_timestamp' => 'NOW()' 
     ); 
     // if result = 0 update 
     if($result == '0') { 
     $this->db->where('item_id',$itemId); 
     $this->db->update('item', $data); 
    } 
     } 

데이터가 데이터베이스에서 업데이트되지 않는 이유가 있습니까? 오류 메시지가 표시되지 않습니다.

도움을 주시면 감사하겠습니다.

+0

실제로 여기에 질문하지 않았습니다 ... –

+0

은 $ 결과 숫자입니까? 위 예제는 숫자를 보여 주지만 여러분은 ==를 문자 '0' – xQbert

답변

1

$query->result()은 각 개체가 테이블의 행인 개체 배열을 반환합니다. 조건부 말했다

if($result->g_data == '0') { ... 

, 당신은 이전 데이터베이스가 atually 결과를 반환하는 방법에 체크인해야해야 다른없이

변경을 (당신은 당신의 의견에 위해서 var_dump에서 볼 수 있듯이). 또한, 다음 조건은 당신이

if($result == '0') {... 
코딩이로 남아있을 수 위를 할 경우 그래서 대신 ') 행 ('

... 
$query = $this->db->get(); 
// The following will set $result to the value of "g_data" if there 
// are rows and to "0" if there are none. 
$result = $query->num_rows() > 0 ? $query->row()->g_data: '0'; 
... 

result() 사용을 사용하지 않는 하나 이상의 행이 필요하지 않습니다