【问题标题】:CodeIgniter activerecord, retrieve last insert id?CodeIgniter activerecord,检索最后一个插入ID?
【发布时间】:2010-12-31 10:54:00
【问题描述】:

是否有任何选项可以在 CodeIgniter 中获取新记录的最后插入 id?

$last_id = $this->db->insert('tablename',
    array('firstcolumn' => 'value',
    'secondcolumn' => 'value')
);

考虑到表格由字段 id(自动增量)firstcolumn 和 secondcolumn 组成。

这样就可以在下面的代码中使用插入id了。

【问题讨论】:

  • 我多次来这个问题。非常感谢!

标签: php codeigniter


【解决方案1】:

真丢脸……

我看了user guide,第一个函数是$this->db->insert_id();

这也适用于 activerecord 插入...

编辑:我更新了链接

【讨论】:

  • 用户指南是此类事情的最佳朋友。我已经使用 codeigniter 2 年多了,但我仍然找到了我不知道的类似功能。
  • 确保将您的插入和此函数包装在事务中,因为查询调度程序可能会在运行此函数之前插入另一个对象
  • 我需要使用insert_id(),但我不知道insert_id() 被授予获取ID 属于insert() 之前!我的意思是,是否可以在另一个请求中检索属于另一个 insert() 的 ID?
【解决方案2】:

Last insert id 表示您可以通过在活动记录中使用此方法获得插入的自动增量 id,

$this->db->insert_id() 
// it can be return insert id it is 
// similar to the mysql_insert_id in core PHP

你可以参考这个链接你可以找到更多的东西。

Information from executing a query

【讨论】:

【解决方案3】:

对于特定表,您不能使用 $this->db->insert_id() 。即使最后一次插入发生在很久以前,它也可以这样获取。可能是错的。但对我来说效果很好

     $this->db->select_max('{primary key}');
     $result= $this->db->get('{table}')->row_array();
     echo $result['{primary key}'];

【讨论】:

    【解决方案4】:

    有助于请求 id 和查询的详细信息列表是

    For fetching Last inserted Id :这将从表中获取最后一条记录

    $this->db->insert_id(); 
    

    获取 SQL 查询在模态请求后添加这个

    $this->db->last_query()
    

    【讨论】:

      【解决方案5】:

      插入查询后,使用此命令$this->db->insert_id(); 返回最后插入的 id。

      例如:

      $this->db->insert('Your_tablename', $your_data);
      
      $last_id =  $this->db->insert_id();
      
      echo $last_id // assume that the last id from the table is 1, after the insert query this value will be 2.
      

      【讨论】:

        【解决方案6】:

        试试这个。

        public function insert_data_function($your_data)
        {
            $this->db->insert("your_table",$your_data);
            $last_id = $this->db->insert_id();
            return $last_id;
        }
        

        【讨论】:

        【解决方案7】:

        $this->db->insert_id();

        试试这个作为你想要的问题。

        【讨论】:

          【解决方案8】:
          $this->db->insert_id();
          

          执行数据库插入时返回插入ID号

          Query Helper Methods 用于 codeigniter-3

          【讨论】:

            【解决方案9】:
            $this->db->insert_id()  
            

            //请试试这个

            【讨论】:

            • 这个重复的答案没有给页面增加新的价值。它应该被删除。
            【解决方案10】:
            if($this->db->insert('Your_tablename', $your_data)) {
                return $this->db->insert_id();
            }
            return false 
            

            【讨论】:

              【解决方案11】:

              在codeigniter 3中,你可以这样做:

              if($this->db->insert("table_name",$table_data)) {
                  $inserted_id = $this->db->insert_id();
                  return $inserted_id;
              }
              

              您可以从这里https://codeigniter.com/user_guide/database/helpers.html获得有关 Codeigniter 3 的帮助

              在 Codeigniter 4 中,您可以获得最后插入的 id,如下所示:

              $builder = $db->table("your table name");
              if($builder->insert($table_data)) {
                  $inserted_id = $db->insertID();
                  return $inserted_id;
              }
              

              您可以从这里https://codeigniter4.github.io/userguide/database/helpers.html获得有关 Codeigniter 4 的帮助

              【讨论】:

                猜你喜欢
                • 2013-11-02
                • 1970-01-01
                • 2011-02-25
                • 1970-01-01
                • 1970-01-01
                • 2015-09-30
                • 1970-01-01
                • 2019-05-20
                相关资源
                最近更新 更多