加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 在CodeIgniter中使用批量插入获取查询的最后一个插入ID时

发布时间:2020-12-13 21:49:03 所属栏目:PHP教程 来源:网络整理
导读:如何使用CodeIgniter中的批量插入获取最后插入的查询ID.我使用了代码$this- db- insert_id()但它返回了我第一个插入数组的ID.我无法获得最后一次插入. 这是我做的: for ($x = 0; $x sizeof($filtername); $x++) { $orders[] = array( 'poid' = null,'order_
如何使用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.

解决方法

你需要做这样的事情,

$insertIds  = array();
for ($x = 0; $x < sizeof($filtername); $x++) {
    $orders = array(
        'poid'              => null,'transaction_date'  => $dateorder
    );
    $this->db->insert('po_order',$orders);
    $insertIds[$x]  = $this->db->insert_id(); //will return the first insert array
}
print_r($insertIds); //print all insert ids

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读