$this->db->insert_id() 在 mysql codeigniter 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6942216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:44:35  来源:igfitidea点击:

$this->db->insert_id() not working in mysql codeigniter

mysqlcodeigniter

提问by Ananth Duari

I am just trying to get the latest auto generated key from the mysql table from codeiginitor. But it is not working some how. I have tried this code
$this->db->insert_id()
as well as
$this->db->mysql_insert_id()
I am getting the following error
Severity: Notice
Message: Undefined property: CI_DB_mysql_driver::$insert_id

is the insert_id function not supported for mysql? How can we make it work?

我只是想从 codeiginitor 的 mysql 表中获取最新的自动生成的密钥。但它在某些方面不起作用。我已经尝试过这段代码
$this->db->insert_id()

$this->db->mysql_insert_id()
但出现以下错误 是mysql不支持insert_id函数吗?我们怎样才能让它发挥作用?
Severity: Notice
Message: Undefined property: CI_DB_mysql_driver::$insert_id

回答by Chris Schmitz

CodeIgniter's insert_id()will only return an ID of an insert(). Unless you are executing something like $this->db->insert('table', $data);before calling the function it will not be able to return an ID.

CodeIgniterinsert_id()只会返回一个insert(). 除非您$this->db->insert('table', $data);在调用函数之前执行类似的操作,否则它将无法返回 ID。

回答by user3337174

Use only this syntax

仅使用此语法

$last_id = $this->db->insert_id();

return $last_id; 

回答by Ananth Duari

This is the code I tried to insert the data and get the insert id,

这是我尝试插入数据并获取插入 ID 的代码,

$this->db->insert('table_name',$data); $id = $this->db->mysql_insert_id();

$this->db->insert('table_name',$data); $id = $this->db->mysql_insert_id();

It is not working for me. I have used DB Driver as "mysql". Then I have installed "mysqli" and changed the DB driver name in database.php like,
$db['default']['dbdriver'] = 'mysqli';
Now it is working fine for me.

它不适合我。我已将数据库驱动程序用作“mysql”。然后我安装了“mysqli”并更改了 database.php 中的数据库驱动程序名称,
$db['default']['dbdriver'] = 'mysqli';
现在它对我来说工作正常。

回答by tanveer javaid

Please check if your table don't have auto increment, primary field then $this->db->insert_id();will not return anything.

请检查您的表是否没有自动增量,那么主字段$this->db->insert_id();将不会返回任何内容。

回答by user1542

assuming you are using active record pattern this should work
$this->db->insert_id()

假设您使用的是活动记录模式,这应该可以工作
$this->db->insert_id()

回答by korvus

I had the same problem, and found a solution with this link: http://kedyr.wordpress.com/2012/10/03/codeigniter-insert_id/

我遇到了同样的问题,并通过此链接找到了解决方案:http: //kedyr.wordpress.com/2012/10/03/codeigniter-insert_id/

The solution is not properly a solution, because it's just a way to get around by using the native sql, with the codeigniter method "Query" and dealing with the native sql callback.

该解决方案不是正确的解决方案,因为它只是通过使用本机 sql,使用 codeigniter 方法“查询”并处​​理本机 sql 回调来绕过的一种方法。

    $this->db->insert('references', $data);
    $query = $this->db->query('SELECT LAST_INSERT_ID()');
    $row = $query->row_array();
    $LastIdInserted = $row['LAST_INSERT_ID()'];

回答by Rudra

// Store data into array
$data=array('name'=>'Rudra','email'=>'[email protected]');

//Insert Data Query
$this->db->insert('tableName',$data);

// Storing id into varialbe
$id = $this->db->insert_id();
return $id;

回答by RAJEEV

try this:

尝试这个:

$id = $this->db->insert_id()

instead of

代替

$id = $this->db->mysql_insert_id();

回答by Faizan Khattak

Try this

尝试这个

$this->db->insert('table_name',$data);
$id = $this->db->mysql_insert_id();

回答by Nugroho Setiawan

Your code will work if you have a code like this :

如果您有这样的代码,您的代码将起作用:

var $table = 'your table name'
$this->db->insert($this->table,$data1);
        return $this->db->insert_id();

CodeIgniter's insert_id() will only return an ID of an insert(), as mention before, and also you dont forget to settings the DB driver name in database.php like

CodeIgniter 的 insert_id() 将只返回一个 insert() 的 ID,如前所述,并且您不要忘记在 database.php 中设置 DB 驱动程序名称,例如

$db['default']['dbdriver'] = 'mysqli';

$db['default']['dbdriver'] = 'mysqli';