php - Codeigniter Select and (conditional) Update Query -


i using codeigniter 3, php , mysql.

i'm trying select record mysql database, , depending on result run update query.

if result = 0, update. if result = 1, nothing. 

my code far is;

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);     }         } 

is there reason data won't update in database? i'm not receiving error messages.

any appreciated.

$query->result() returns array of objects each object row table. (as can see in var_dump in comments)

without other changes conditional should be

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

that said, should have checked earlier in method database atually returned results. also, don't need more 1 row don't use result() use 'row()' instead

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

if above conditional can remain have coded

if($result == '0') {... 

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -