Welcome Guest, Not a member yet? Register   Sign In
insert_id not working
#1

[eluser]Fred Riley[/eluser]
I've a feeling that this question will result in a D'OH!! but it's late at night, I've spent two hours on this problem and I need my beauty sleep. My problem is that I can't get the simple insert_id() to work, eg:

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

A user registers (I'm using ion-auth), the row in the users table is inserted, but insert_id stubbornly returns 0 no matter what the key of the inserted row. Other query helper functions work, eg:

$rows = $this->db->affected_rows();

This returns 1 after the operation, as you'd expect. The database library is autoloaded. I've read TFM on Active Record and Query Helper. I've searched the forum but could only find one maybe relevant thread which doesn't have the answer. All I'm after doing is getting basic details of the user just registered to display.

Two questions:

1. What am I doing wrong, or missing?
2. Is the query helper integral to CI or does it have to be loaded?

Thanks in advance Smile

Fred

PS: In a previous application written in CI 1.7 insert_id worked fine.
#2

[eluser]monoclonal[/eluser]
Can you post the ten lines of code before $this->db->insert_id(); ?
#3

[eluser]cPage[/eluser]
MySQL 5.0 Reference Manual

20.6.10.3. How to Get the Unique ID for the Last Inserted Row
If you insert a record into a table that contains an AUTO_INCREMENT column, you can obtain the value stored into that column by calling the mysql_insert_id() function.

Code:
//--CI
$this->db->insert_id()
#4

[eluser]Harold Villacorte[/eluser]
Open up the file system/core/database/DB_active_rec.php and search for "insert_id". Might not be part Active Record.
#5

[eluser]Aken[/eluser]
Always use insert_id() directly after your query. Adding code in between could mess with the results.

Make sure the database table you're inserting into actually has a primary key. If it doesn't, insert_id() won't work.

Active Record is used by at least couple libraries (Session being the main one). It is not required to use in your application, but it is used by other CI components, yes.
#6

[eluser]cPage[/eluser]
Or this in your model, if you dont have primary key
Code:
function last_id()
{
$int = $this->db->count_all($this->table);
$query = $this->db->get($this->table,1,--$int);
return $query->row()->id;
}
#7

[eluser]monoclonal[/eluser]
Quote:Or this in your model, if you dont have primary key

function last_id()
{
$int = $this->db->count_all($this->table);
$query = $this->db->get($this->table,1,--$int);
return $query->row()->id;
}

completely unnecessary. Just use $this->db->insert_id(); immediately after $this->db->insert();
easy.
#8

[eluser]Harold Villacorte[/eluser]
I am on 2.1.3 and insert_id() is not in the codebase as far as I can tell.
#9

[eluser]Aken[/eluser]
[quote author="Harold Vilacorte" date="1359096264"]I am on 2.1.3 and insert_id() is not in the codebase as far as I can tell. [/quote]
It's in the individual driver files. The feature has been around for a long time.
#10

[eluser]Harold Villacorte[/eluser]
[quote author="Aken" date="1359102259"][quote author="Harold Vilacorte" date="1359096264"]I am on 2.1.3 and insert_id() is not in the codebase as far as I can tell. [/quote]
It's in the individual driver files. The feature has been around for a long time.[/quote]

Ok. Found it. That CI_DB class is still eluding me though. I will have to search for it later, gotta get ready for work.




Theme © iAndrew 2016 - Forum software by © MyBB