如果主,自动递增字段的列名不是id,可以使用$this->db->insert_id()函数


If the column name of primary ,auto increment field is not id , can $this->db->insert_id() function be used

我是Codeigniter的新手。

所以这是我的表:

request_id | login_name | login_password | reg_date | 状态

如果主要的自动增量列名称是request_id而不是id,我可以使用$this->db->insert_id()来获取最后插入的 id 吗?

任何帮助将不胜感激。

是的,MySQLi 中的insert_id不关心列名,只关心是AUTO_INCREMENT的。它返回;

上一个查询更新的AUTO_INCREMENT字段的值。如果连接上没有以前的查询,或者查询未更新AUTO_INCREMENT值,则返回零。

是的,你可以使用。

$this->db->insert_id()为您提供上次插入记录的 ID,因此无需更改方法名称。

参考这里

是的,使用 $this->db->insert_id(),您可以获取表中上次插入记录的 ID。
喜欢这个:

     <?php    
    class InsertRecord extends CI_Model {
    var $tablename = "test_table";
    var $primaryID = "request_id";
        function insertRecord(){
             $insertArr['login_name'] = $_POST['login_name'];
             $insertArr['login_password'] = $_POST['login_password'];
             $insertArr['reg_date'] = $_POST['reg_date'];       
             $insertArr['status'] = $_POST['status'];
             if ($this->db->insert($this->tablename, $insertArr)) {
                $lastInsertedID = $this->db->insert_id();
             }
        }
     }
    ?>

在变量$lastInsertedID,您将获得上次插入记录的 id。

希望这对你有帮助... :)