Log in

View Full Version : Resolved Get Unique Id Or Last Inserted Id



hemi519
11-16-2012, 11:01 AM
Hi All,


Can anyone tell me how can i get a last inserted unique user id. Following is my Scenario

Every-time a user is created in my website, all his info will be stored in "user" table except his image details. For image, i am using another table "user_images".





mysql_query("INSERT INTO `user` (username,password,age,gender) Values ('avc','****','12','1')"); // First user info enters into table
$userid = mysql_insert_id(); // bring last inserted id.
mysql_query ("INSERT INTO `user_images`(`user_id`, `title`, `original_filename`, `stored_filename`, `content_type`)VALUES ('$userid','$title','$realname','$filename','$contenttype')"); // inserted the row with mysql_insert_id as userid
// user_id field is unique in the table




Now, my only issue is if two users are doing this at a time. Is there a possibility of they might be having same "mysql_insert_id" and as user_id in "user_images" table is unique, one user ends up with not storing his image info. Can anyone tell me if it is the correct way to do it?

If there is anyother way to do this, please suggest me.

traq
11-16-2012, 03:11 PM
--------------------------------------------------
# If at all possible, you should avoid using the mysql_* functions. #
ext/mysql is outdated and scheduled for deprecation. It is no longer recommended for new projects, and existing code should be updated to avoid performance and security problems. Using ext/mysqli (http://php.net/mysqli) or the PDO class (http://php.net/pdo) is recommended. Read more about choosing an API (http://php.net/mysqlinfo.api.choosing) here.

--------------------------------------------------
If you want to make sure you get the correct id, and username (or some other column) is unique, you can simply query for it explicitly:
INSERT INTO `user` (username,password,age,gender) Values ('avc','****','12','1');
SELECT `id` FROM `user` WHERE `username`='avc';

If you were using the INNODB engine, and ext/mysqli (http://php.net/mysqli), you could get the id in a transaction:
BEGIN;
INSERT INTO `user` (username,password,age,gender) Values ('avc','****','12','1');
SELECT LAST_INSERT_ID() `id`;
COMMIT;You can do this with PDO (http://php.net/pdo) also, and PDO can emulate the transaction if you're not using an engine that supports transactions (e.g., the default MyISAM engine)(though I hear the emulation is buggy sometimes).

hemi519
11-16-2012, 03:56 PM
Thanks for the info. For now i will go with following because i am using MyIsam

INSERT INTO `user` (username,password,age,gender) Values ('avc','****','12','1');
SELECT `id` FROM `user` WHERE `username`='avc';

traq
11-17-2012, 02:17 AM
No problem; glad to help. As long as the username field is unique, that should work fine.

If your question has been answered, please mark your thread "resolved":
On your original post (post #1), click [edit], then click [go advanced]. In the "thread prefix" box, select "Resolved". Click [save changes].