【问题标题】:If the column name of primary ,auto increment field is not id , can $this->db->insert_id() function be used如果 primary ,auto increment 字段的列名不是 id ,可以使用 $this->db->insert_id() 函数
【发布时间】:2013-01-24 05:24:37
【问题描述】:
我是 Codeigniter 的新手..
这是我的桌子:
request_id |登录名 |登录密码 |注册日期 |状态
如果主自动增量列名称是 request_id 而不是 id,我可以使用 $this->db->insert_id() 获取最后插入的 id 吗?
任何帮助将不胜感激..
【问题讨论】:
标签:
php
mysql
codeigniter
mysql-insert-id
【解决方案1】:
是的,MySQLi 中的insert_id 不关心列名,只是该列是AUTO_INCREMENT。它返回;
由上一个查询更新的 AUTO_INCREMENT 字段的值。如果连接上没有先前的查询或查询没有更新 AUTO_INCREMENT 值,则返回零。
【解决方案2】:
是的,您可以使用。
$this->db->insert_id() 为您提供最后插入记录的 id,因此无需更改方法名称。
参考here
【解决方案3】:
是的,使用 $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。
希望这会对你有所帮助... :)