Advanced Search

Results 1 to 4 of 4

Thread: Get Unique Id Or Last Inserted Id

  1. #1
    Join Date
    Nov 2010
    Posts
    114
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Get Unique Id Or Last Inserted Id

    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".


    PHP Code:

    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.
    Last edited by hemi519; 11-17-2012 at 05:43 AM.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,629
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    --------------------------------------------------
    # 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 or the PDO class is recommended. Read more about choosing an API 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:
    Code:
    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, you could get the id in a transaction:
    Code:
    BEGIN;
    INSERT INTO `user` (username,password,age,gender) Values ('avc','****','12','1');
    SELECT LAST_INSERT_ID() `id`;
    COMMIT;
    You can do this with 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).
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

  3. The Following User Says Thank You to traq For This Useful Post:

    hemi519 (11-16-2012)

  4. #3
    Join Date
    Nov 2010
    Posts
    114
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default

    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';

  5. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,629
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    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].
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

Similar Threads

  1. Replies: 0
    Last Post: 02-10-2011, 04:29 PM
  2. char vars may inserted as method parameters ?
    By leonidassavvides in forum Java
    Replies: 1
    Last Post: 08-25-2009, 01:55 PM
  3. 5 tabs but 1 Unique tab
    By hughvia3 in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 07-03-2008, 12:54 AM
  4. Replies: 11
    Last Post: 06-15-2008, 04:13 PM
  5. Change ScrollBar Color inserted inside table
    By pinkpanther in forum CSS
    Replies: 4
    Last Post: 10-26-2006, 07:32 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •