Welcome Guest, Not a member yet? Register   Sign In
[SOLVED]Last ID from a table
#1

[eluser]Unknown[/eluser]
Hi there guys, this's my secound day with CI. I'm stuck my problem is this

I have 2 tables orders and orderdetail they are connected with an ID

Orders orderdetail

order_detail_id
order_id order_id
date quantity
time price
... ...

I create the record in the DB then I need it to save it into the orderdetail table
I tried using $this->db->insert_id() and it works but, I was thinking... what if there are 2 or more people trying to access... eventually another person could generate a new id before the $this->db->insert_id() was fired or the $this->db->insert_id() is connected with the previous insert query? I don't wanna use InnoDB, maybe using transactions to create the record in the Orders table and the N records in the orderdetail table

the other option that a frinend suggested to me its use the time and date to know wich record select... but it could return a ducplicate value too...

Any advice or suggestions? Thanks
#2

[eluser]bretticus[/eluser]
If you are using MySQL and you dig under the hood a bit I'd suspect you'd find that insert_id probably utilizes mysql_insert_id and that PHP function makes use of the mysql_insert_id function that is native to MySQL. According to the MySQL manual:

Quote:For LAST_INSERT_ID(), the most recently generated ID is maintained in the server on a per-connection basis. It is not changed by another client. It is not even changed if you update another AUTO_INCREMENT column with a nonmagic value (that is, a value that is not NULL and not 0). Using LAST_INSERT_ID() and AUTO_INCREMENT columns simultaneously from multiple clients is perfectly valid. Each client will receive the last inserted ID for the last statement that client executed.

Each page load or each time the browser requests a URI and the PHP code at that URI is executed, you have a client instance in context with the rest of the script being executed. In other words, mysql_insert_id should return the last query relative the instance in which it is running. Not another users instance of that same script.

In 10 years of PHP development, I cannot think of a single time that nearly simultaneous page loads stole a last insert id. I don't think you have cause for concern in this case.
#3

[eluser]richthegeek[/eluser]
[strike]It is a hugely unlikely scenario, but at any rate the orders will be tied to a customer_id so you could select the MAX(order_id) using that?[/strike]

Nevermind, what the bretticus said.
#4

[eluser]Unknown[/eluser]
Thank you bretticus! I really appreciate your knowledge, with this, I feel safety know.

Juts to make it official getting unique id

Quote:For LAST_INSERT_ID(), the most recently generated ID is maintained in the server on a per-connection basis. It is not changed by another client. It is not even changed if you update another AUTO_INCREMENT column with a nonmagic value (that is, a value that is not NULL and not 0). Using LAST_INSERT_ID() and AUTO_INCREMENT columns simultaneously from multiple clients is perfectly valid. Each client will receive the last inserted ID for the last statement that client executed.




Theme © iAndrew 2016 - Forum software by © MyBB