Codeigniter affected_rows always returns 0 when rows are inserted

Im doing an insert to the table and the script should only continue if the update is inserted correctly.

if($this->db->affected_rows() > 0){}

      

Works fine if in my application, but for some reason at some point it doesn't and always returns 0

the below snippet

while($asked_order_amount_needed > 0){

$matches_attempted = 0;
$matched_orders = $query->result_array();
foreach($matched_orders as $matched_order){

//we can fill the submitted order completely with a matched order with a higher volume then the ask volume so try this first
if($matched_order['amount'] >= $ask['amount']){


$ask_history = array(
'order_type' => $ask['order_type'],
 'amount' => $ask['amount'],
 'price' => $ask['price'],
 'entry_by' => $ask['entry_by'],
 'status' => 'filled',
 'buy_currency_name' => $ask['buy_currency_name'],
 'sell_currency_name' => $ask['sell_currency_name'],
 'timestamp' => date('Y-m-d H:i:s')
);

$dump = $this->db->insert('tb_order_book_history', $ask_history);
//$sql = $this->db->last_query();

if($this->db->affected_rows() > 0){

      

I added $dump =

infront of$this->db->insert('tb_order_book_history',$ask_history);

Just for debugging to test the output that returns true and I copied the value $sql

$sql = $this->db->last_query();

      

run it through the MySQL desktop, which inserts it ok, andver also checked the table after starting the application through this function, and the data is inserted fine. Now, if I haven't looked at the code too long and missed something really simple, I have no idea why it behaves this way.

Any ideas highly appreciated.

+3


source to share


1 answer


Instead $this->db->affected_rows()

why don't you try it along with $this->db->insert_id()

. You can use it just like



$this->db->insert('tb_order_book_history',$ask_history);
$dump = $this->db->insert_id();

if(!empty($dump) && $dump > 0){
    // your code
}

      

+1


source







All Articles