Error in getting the last inserted ID of a query using batch insert in CodeIgniter

前端 未结 2 1084
隐瞒了意图╮
隐瞒了意图╮ 2021-01-14 08:12

How can I get the last inserted ID of a query using the batch insert in CodeIgniter. I used the code $this->db->insert_id() but it returns the ID of my fi

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-14 08:43

    I think the best way would be to use the batch insert instead of individual inserts in a loop for performance , but to get the last insert id, ADD the First Insert ID & the Affected Rows.

    $this->db->insert_batch('po_order', $orders);
    $total_affected_rows = $this->db->affected_rows();
    $first_insert_id = $this->db->insert_id();
    
    $last_id = ($first_insert_id + $total_affected_rows - 1);
    

提交回复
热议问题