Welcome Guest, Not a member yet? Register   Sign In
How to get last insert ID in a batch insert?
#1

[eluser]rochellecanale14[/eluser]
Hello guys I just want to ask how can I get the last inserted ID using the $this->db->insert_id() function. What I did is after my $this->db->insert_id() and assigned it in a variable, when I echo the value of it. It display the first index ID of my array. I can't get the last inserted.

Here's my code:

Code:
for($x = 0; $x < sizeof($filtername); $x++){
                
                $orders[] = array(
                    'poid' => null,
                    'order_id' => $poid,
                    'item_desc' => $filtername[$x],
                    'item_qty' => $filterquantity[$x],
                    'item_price' => $filterprice[$x],
                    'total' => $filtertotal[$x],
                    'cash_on_delivery' => $val_delivery,
                    'is_check' => $val_check,
                    'bank_transfer' => $val_transfer,
                    'transaction_date' => $dateorder
                );
                
            }

            $this->db->insert_batch('po_order', $orders);
            $v_poid = $this->db->insert_id(); //this will return the first inserted ID of my array
            
            echo $v_poid;
            exit;
#2

[eluser]CroNiX[/eluser]
You can't get them all from a batch command, just like you can't accurately get affected_rows (it will only report the successful rows from the last part of the batch, not entire batch). If you need all of the IDs you'll have to insert individual rows and get the insert_id for each as it executes.




Theme © iAndrew 2016 - Forum software by © MyBB