【问题标题】:Store LAST_INSERT_ID() in a transaction将 LAST_INSERT_ID() 存储在事务中
【发布时间】:2011-02-27 03:06:13
【问题描述】:

我使用codeigniter 的数据库抽象,并与它进行交易。我的问题是,我有几个插入到几个表中,但我需要第一个插入查询中的插入 ID。有没有办法为多个后续插入存储最后一个插入 id?

我不明白为什么,但是 ci 内置函数不起作用。

【问题讨论】:

    标签: php mysql codeigniter transactions


    【解决方案1】:

    在您完成查询后立即获取 insert_id...

    $this->db->insert('table1', $data);
    $insert_id1 = $this->db->insert_id();
    
    $this->db->insert('table2', $data);
    $insert_id2 = $this->db->insert_id();
    
    $this->db->insert('table3', $data);
    $insert_id3 = $this->db->insert_id();
    

    这是最简单的方法。

    【讨论】:

    • 好的,谢谢 :) 我只是不知道如何使用 insert_id() 方法。
    • 我想知道这两个语句是否应该包装在一个事务中。如果他们不这样做,这仍然安全吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    相关资源
    最近更新 更多