Welcome Guest, Not a member yet? Register   Sign In
get insert id - SOLVED - this->db->insert_id()
#1

[eluser]FuzzyJared[/eluser]
Is there a function for getting the the id of the insert that you just performed?

I am doing a multipart application, and need to get the insert id from one insert before I perform the next one.

I have looked around for this, but I am unclear if this exists within the db class or if I would have just do a sql query by hand.
#2

[eluser]mironcho[/eluser]
There is such method:
Code:
$this->db->insert_id()

http://ellislab.com/codeigniter/user-gui...lpers.html
#3

[eluser]Derrick24[/eluser]
How safe is this function guys, if user_a and user_b both post at the same time, will user_a get user_b's last inserted id?
#4

[eluser]Colin Williams[/eluser]
Quote:Note: Because mysql_insert_id() acts on the last performed query, be sure to call mysql_insert_id() immediately after the query that generates the value.

http://php.net/manual/en/function.mysql-insert-id.php

If you do it on, say, the next immediate line, it's going to be accurate to the millisecond (thereabouts). I personally have never had anything funky go on.
#5

[eluser]Derrick24[/eluser]
cool thanks
#6

[eluser]Imran Tariq[/eluser]
Thanks
#7

[eluser]mikemike[/eluser]
I don't think it works that way, I'm pretty sure it's linked to the DB connection resource.

So, in theory, you could make an insert at the top of your code, then have the page do something time consuming for 10 minutes and call the function at the bottom of the page. No matter if someone else inserted something in the mean time, the value of the function would be the same, because it's the last inserted ID of that connection.

In the years I've spent doing this stuff, I've never once had any issues with inserted id's - and I've worked on site with many hundreds of thousands of unique visits a day.




Theme © iAndrew 2016 - Forum software by © MyBB