SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jul 2001
    Location
    San Francisco Bay Area
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Generating Unique Ids

    Is anyone aware of a good way to generate unique ids for sessions and id columns in a database? I'm currently using mysql which doesn't have a pre-build unique id function like mssql. Any suggestions would be helpful.

    Webpenner

  2. #2
    I'm a college yuppie now! sbdi's Avatar
    Join Date
    Jul 2000
    Location
    Dublin , Ireland
    Posts
    1,271
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I dont know PHP or have never used MySQL, only reason I'm here was cause its on the main page . So this is just a guess, but cant you make the field in the database a autonumber that way it will create a new number every time.
    Back Again

  3. #3
    SitePoint Member
    Join Date
    Jul 2001
    Location
    San Francisco Bay Area
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I could use that options but I'm trying to get a unique id that has the same number of characters everytime. Using the auto increment with mysql gives you 1,2,3,...15,16,...290,291,etc. I want a unique id with the same amount of characters each time. Thanks for your suggestion.

    Webpenner

  4. #4
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you just want them to be the same length and the consecutive numbering is ok, just make it a ZEROFILL column (they'll be left padded w/ 0s, if that's all right). if you want them 5 long, for example, it'd be like this.

    Code:
    CREATE TABLE table (
       id INT(5) UNSIGNED ZEROFILL NOT NULL PRIMARY KEY
    )
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  5. #5
    Database Jedi MattR's Avatar
    Join Date
    Jan 2001
    Location
    buried in the database shell (Washington, DC)
    Posts
    1,107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want a random unique ID and not necessarily an auto-incrementing integer there are a number of ways to do it.

    You can try using PHP's uniqid function, or you can try some of the things noted in this neat-o Sybase paper:
    http://my.sybase.com/detail?id=860

  6. #6
    This is all Mumbo Jumbo Amit's Avatar
    Join Date
    Oct 2001
    Location
    New Delhi, India
    Posts
    277
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    You can use uniqid as Matt told, for even more random ids try this ....

    Code:
    srand( microtime() );
    $session = md5( uniqid( rand(), true ) );  // 1
    $session = uniqid( rand(), true );  // 2
    1 would give you a 32 byte long string ...
    2 would give you a longer one ...


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
  •