Welcome Guest, Not a member yet? Register   Sign In
Get the id of last insert (mysql)
#1

[eluser]Bonkie[/eluser]
Is it possible to get the id of the data i've inserted into the MySQL DB?

My SQL code:

$sql = "INSERT INTO items (name, `desc`, cid, uid, uri, dateplaced) VALUES(?,?,?,?,?, NOW())";
#2

[eluser]Pascal Kriete[/eluser]
If you're using active record, you can do:
Code:
$this->db->insert_id()

Found here.
#3

[eluser]Glen Swinfield[/eluser]
If you need to use the php function rather than the db object method, the function is

$id = mysql_insert_id()

with an optional connection identifier as a parameter.
#4

[eluser]xwero[/eluser]
or you can do
Code:
$id = $this->db->call_function('insert_id');
But inparo made a little mistake the insert_id method is not a AR method.
#5

[eluser]Bonkie[/eluser]
Thanks for the fast replies.

I use
Code:
$this->db->insert_id()
now and it works perfectly for now.

But i'll try yours xwero
#6

[eluser]xwero[/eluser]
Code:
$this->db->insert_id()
Is the fastest method because it does doe checks. it's a wrapper for the database specific native php function that gets the last inserted id. The call_function method does some additional checks which makes it slower. The function is intended for database specific functions that can't or are hard to abstract for the cross database library CI provides.
#7

[eluser]Bonkie[/eluser]
[quote author="xwero" date="1206547409"]
Code:
$this->db->insert_id()
Is the fastest method because it does doe checks. it's a wrapper for the database specific native php function that gets the last inserted id. The call_function method does some additional checks which makes it slower. The function is intended for database specific functions that can't or are hard to abstract for the cross database library CI provides.[/quote]


What is the best option to use?
The directly PHP function wrapper or the call_function ??
#8

[eluser]xwero[/eluser]
$this->db->insert_id();

You best choose performance above security if you need to call a function. The wrapper already slows down your application a tiny bit. The more checks you add the sooner you start noticing the application slowness.
#9

[eluser]Bonkie[/eluser]
Well its for a ebay like site.
So in my opinion the security goes first cuz of the platform aint it?
#10

[eluser]xwero[/eluser]
The security for functions is checking if they exists and if they are callable. These things you find out while you are developing and test the site on the production server. So you as a developer do the checks yourself. Functions don't suddenly disappear from your server.




Theme © iAndrew 2016 - Forum software by © MyBB