Page 1 of 1

Create unique ID

Posted: Mon May 12, 2008 7:16 pm
by Louic
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hello

This is my first post here at phpdn. Hope I'll make it right. Now to my problem.

I wan't to create a unique ID and save it down to my MySQL. The problem is that I don't now how to make the code loop. I'll post my code with explanation.

Code: Select all

           function slumpa() {
                $s = chr(mt_rand(97, 122)) . chr(mt_rand(65, 90));
                for ($i=0; $i<20;$i++) $s.= mt_rand(0, 9);
            return $s;
            } 
    
            $regid = slumpa()
            
                $sql = "SELECT COUNT(hashid) FROM medlemmar WHERE hashid='{$slumpa}'";
                $result = mysql_query($sql);
                  if (mysql_result($result, 0) > 0) {
                    // Find a new  unique ID and check if it is in the databas, if it is, make this until finding a unique one.
                  }

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: Create unique ID

Posted: Mon May 12, 2008 7:32 pm
by Christopher
You might want to just use uniqid()... ;) Also, and auto_increment integer column is probably a better solution for a unique ID in a database table.

Re: Create unique ID

Posted: Mon May 12, 2008 8:32 pm
by Louic
Hello and thanks for the answer! Yes I know the auto_increment "method" but I need it to be this way I explained. It should be a hash in a login function and every user should have a unique one.

Any suggestions on have to solve my problem?