schenker schenker - 1 month ago 15
PHP Question

How can i convert this PDO statement to active record query in CodeIgniter?

i have this PDO statement that works fine, but how can i convert it to active record in CodeIgniter?

My PDO statement is:

UPDATE mytable set total=bought+re_order-balance where retail_id=? and stock=? and stock_date=?


I tried this, but didnt work:

$data=array(
//some other fields
'total'=>'bought' + 're_order' - 'balance'
);

$this->db->where('retail_id', $someid);
$this->db->where('stock', $somestock);
$this->db->where('stock_date', $somestock);
$this->db->update('mytable',$data);


When i tried this, it didn't give any errors at the same time, it did not update my total field in the database. Any help pls. Thanks.

Answer

The following code should build the needed query. The third parameter of set() method prevents data from escaping. You can read more abou it in the docs

$this->db->set('total', 'bought + re_order - balance', FALSE)
            ->where('retail_id', $someid)
            ->where('stock', $somestock)
            ->where('stock_date', $somestock)
            ->update('mytable');