#1
  1. Full Access
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jun 2000
    Location
    London, UK
    Posts
    2,019
    Rep Power
    16
    I am working on a project that requires each article to be assigned a unique nine-digit ID number that will be stored in a mySQL database along with some other info. I know I can use the PRIMARY KEY function to make sure it's unique in mySQL but my problem lies in the fact that I don't know how to generate a random 9-digit number in PHP! Can someone please help me out here? Thanks.

    --------------------
    Alex
    (http://www.alex-greg.co.uk)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 1999
    Posts
    114
    Rep Power
    16
    Have a look at the various random number functions at:
    http://www.php.net/manual/function.srand.php
  4. #3
  5. Full Access
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jun 2000
    Location
    London, UK
    Posts
    2,019
    Rep Power
    16
    The point you're missing is that it always has to be nine digits long: with no zeros at the start. How do I do this?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 1999
    Posts
    114
    Rep Power
    16
    Doesn't rand (100000000, 999999999) work?
  8. #5
  9. Full Access
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jun 2000
    Location
    London, UK
    Posts
    2,019
    Rep Power
    16
    OK that now works, thanks, but the number stays the same even when I refresh the page: how can I get it so that the number's different each time?
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    6
    Rep Power
    0
    You should be able to find that information at the link scollo posted..
  12. #7
  13. No Profile Picture
    Gödelian monster
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 1999
    Location
    Central Florida, USA
    Posts
    2,307
    Rep Power
    61
    Why not just generate a number based on the current date, time, & second? It also might provide a useful way of tracking items later.
  14. #8
  15. Full Access
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jun 2000
    Location
    London, UK
    Posts
    2,019
    Rep Power
    16
    OK, how does that work? Don't forget you're dealing with a novice here!
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 1999
    Posts
    114
    Rep Power
    16
    Hi Alex,

    Sorry. Didn't mean to frustrate you. You need to call the srand() function before you call the rand() function. This causes the number to be different every time.

    Try this:

    srand (time());
    $iNum = rand (100000000, 999999999);

    -- Christopher
  18. #10
  19. Full Access
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jun 2000
    Location
    London, UK
    Posts
    2,019
    Rep Power
    16
    Sweet, it works. Thanks guys!

    [Topic closed]

Similar Threads

  1. MYSQL, php, random generate
    By LeBoOo in forum PHP Development
    Replies: 2
    Last Post: September 30th, 2004, 01:47 PM
  2. How to see the Memory used by a PHP script?
    By Pardall in forum PHP Development
    Replies: 4
    Last Post: February 13th, 2004, 03:54 PM
  3. Can php be used in html files
    By emzyme in forum PHP Development
    Replies: 3
    Last Post: January 29th, 2004, 01:50 PM
  4. Replies: 4
    Last Post: January 28th, 2004, 08:29 AM
  5. random number script
    By sander1971 in forum ASP Programming
    Replies: 4
    Last Post: January 27th, 2004, 09:48 AM

IMN logo majestic logo threadwatch logo seochat tools logo