Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    Regular Coder
    Join Date
    Jan 2009
    Posts
    173
    Thanks
    5
    Thanked 12 Times in 11 Posts

    Question Checking if row exists in database, and if it does, try again with another string?

    Hi.

    To begin with, i just wanna say that i wasn't sure whether to post this in the PHP or MySQL forum. So if i posted it wrong, please move it

    I need some help. As it says in the title what i'm trying to do is to generate a random number (a big one, since it has to be unic!), and then check if it's already used in that particular database table. IF it already is used, then generate a new random number and check again, and i want it to continue looping until it finds a number that is not used in the database.

    Here's the code i currently have:

    PHP Code:
    //Generate a random number and store it in $unic_id
    $unic_id rand(0,10000000000);

    //try to select the number in $unic_id in the database table
    $sql mysql_query('SELECT unic_id FROM users WHERE unic_id="' $unic_id '"');
    $unicId_array mysql_fetch_array($sql);

    //Checks if $unicId_array has a value set to it, and if it does, generate a random number etc...
    if(isset($unicId_array['unic_id'])){
        while(isset(
    $unicId_array['unic_id'])){
            
    //gör ett nytt random nummer
            
    $unic_id rand(0,10000000000);
            
            
    //Check if the new $unic_id exists
            
    $sql mysql_query('SELECT unic_id FROM users WHERE unic_id="' $unic_id '"');
            
    $unicId_array mysql_fetch_array($sql);
        }

    Unfortunately it isn't working


    Thanks in advance.
    -Nike
    Last edited by nikee; 03-22-2010 at 03:10 PM.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    If you take a random number that is like 6 numbers long,
    and append it to time(), it will never be repeated. You
    would not even have to check.

    time(), which is UNIX time, is a 10 digit number.
    Example, right now it's about: 1269215080

    So you could append 3 numbers in front, 3 in back,
    or 6 in front, or 6 in back ... no matter how you append
    any more random numbers or letters, it will never be the same.

    This UNIX timestamp ( 1269215080 ) only existed for 1 second,
    and it will never exist again.

  • #3
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Use uniqid() it will create a unique ID each time. You can add a prefix to it also if needed.
    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

  • #4
    Regular Coder
    Join Date
    Jan 2009
    Posts
    173
    Thanks
    5
    Thanked 12 Times in 11 Posts
    Ohh, why didn't i think of that? *sigh* haha!

    So, something like this would do?

    PHP Code:
    $threeFirst rand(100,999); 
    $unicId date(YmdHisu) . $threeFirst;//that's like: 20100322152108654321 + those three last ones... damn! :D 
    @masterofollies; i already have another column that auto increases, if that's what you mean?
    Last edited by nikee; 03-22-2010 at 03:23 PM.

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    auto-increment ID is incrementing.
    unique ID is random, but never repeating (I think that statement is correct?)

    or, using date() or time() ... the final result is to get a unique, non-repeating number.

  • #6
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Quote Originally Posted by mlseim View Post
    unique ID is random, but never repeating (I think that statement is correct?)
    Correct, if it matches it changes it's self. So you never have to worry about a double.
    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

  • #7
    Regular Coder
    Join Date
    Jan 2009
    Posts
    173
    Thanks
    5
    Thanked 12 Times in 11 Posts
    That sounds pretty good! It doesn't seem to want to be applied though

    Any idea what's up?
    (sorry most of it is in swedish)

  • #8
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    It tells you what the error is. You're trying to insert a second value with a value of 0.

  • #9
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    empty all the id numbers before adding the auto int

  • #10
    Regular Coder
    Join Date
    Jan 2009
    Posts
    173
    Thanks
    5
    Thanked 12 Times in 11 Posts
    Yeah i saw the error, and i understood it. But what didn't make sense is that because the column doesn't exist, i can't empty them! I had 4 users registered (i'm making a forum if anybody's wondering), and i had to remove them all for it to work. I added it now and it works as expected!

    There was however something else i was going to ask, but i kind of forgot it :$

  • #11
    Regular Coder
    Join Date
    Jan 2009
    Posts
    173
    Thanks
    5
    Thanked 12 Times in 11 Posts
    Ohh noo!

    It's actually not working as expected, at all!

    When writing to the database, i get this error:

    Code:
    Duplicate entry '0' for key 'unique_id'
    So it doesn't seem to be getting an unique id after all, huh?

    EDIT: If i manually set unique_id to something that doesn't exist, like 5 for example (since i only have one user, with the unique_id of 0), then it works. But if i just leave it blank, like '', then it i get that dis-liked error message.
    Last edited by nikee; 03-22-2010 at 06:50 PM.

  • #12
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    from the picture you are setting the index to uniuqe and not setting the auto int which is just under it and if its an id the it would be a primary key in the index dropdown.

  • #13
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Making a forum will take a good year of work to make. I suggest download a forum and customizing it.
    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

  • #14
    Regular Coder
    Join Date
    Jan 2009
    Posts
    173
    Thanks
    5
    Thanked 12 Times in 11 Posts
    @DJCMBear, i already have a primary key, called just id. But i'll use the date() method instead.

    @masterofollies, making a forum isn't THAT hard. Believe it or not, but i'm pretty much done with mine now! The reason i'm coding it myself is for two reasons; 1) It's a school work, so i have to do it, haha. And 2) It's fun and instructive! I've learned a few very important things while creating this forum. Before creating it, i didn't even know how to connect to a database, in fact, the only thing i knew in PHP was how to include another file, and how to work with really basic variables. It took me 2 weeks to finish it. I'd show you if i could, but i have it on localhost right now.

    Oh and i've been using both vBulletin and IP board.

  • #15
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by nikee View Post
    @masterofollies, making a forum isn't THAT hard. Believe it or not, but i'm pretty much done with mine now! The reason i'm coding it myself is for two reasons; 1) It's a school work, so i have to do it, haha. And 2) It's fun and instructive! I've learned a few very important things while creating this forum. Before creating it, i didn't even know how to connect to a database, in fact, the only thing i knew in PHP was how to include another file, and how to work with really basic variables. It took me 2 weeks to finish it.
    He's referring to the fact that there is a big difference between coding something up that you can use as a forum and coding something up that is both secure, bug free, (as much as any software ever can be, that is), and fully functioning forum. The former is, as you mention, a simple enough task. I doubt there's any on here who would suggest taking less than several months, (absolute minimum), to achieve the latter, however.

    Good to see someone knuckling down to producing something and enjoying doing it, however. Well done.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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