如何使用 CodeIgniter 中的批量插入获取查询的最后插入 ID。我用了代码$this->db->insert_id()
但它返回我第一个插入的数组的 ID。我无法获取最后一个插入内容。
这就是我所做的:
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);
echo $this->db->insert_id(); //will return the first insert array
我无法发现我的错误在哪里。我的最后一个选择是使用查询来获取它。我也做了mysql_insert_id()
但总是返回0。
我认为最好的方法是使用批量插入而不是循环中的单独插入以提高性能,但要获取最后一个插入 ID,请添加第一个插入 ID 和受影响的行。
$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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)