Page 1 of 1

Create a unique id and add that to mysql database

Posted: Tue Feb 22, 2011 3:42 pm
by izeqb
Hi guys...

I need to generate a 3-4 letter unique id and add it to a database.
I'm not sure how many characters there is, that I can use (I'm guessing around 128 or 256)... If there's around 100, 3 digits would be enough.

So, here'e what I need it for:

I have a php script that adds a records to a "log-database" each time it's activated. Now, I want to add a unique id also... I son't think I can use the "id" field from the mysql databse, because it's only numbers and that wouldn't be enough....

Also, I need the ID before I save the data to the database.

Any ideas?

Re: Create a unique id and add that to mysql database

Posted: Tue Feb 22, 2011 4:07 pm
by John Cartwright
Why not create the row in the db, get the id using mysql_insert_id(), process whatever, then update the record?

Re: Create a unique id and add that to mysql database

Posted: Tue Feb 22, 2011 4:48 pm
by izeqb
John Cartwright wrote:Why not create the row in the db, get the id using mysql_insert_id(), process whatever, then update the record?
My main problem here is generating the id. I would like to somehow, create an ascending letter id, like: aaaa, aaab, aaac, aaad, etc.

I was thinking that I could fetch the prior id from the database and then use a function (or something else?) to generate the next id in the line.
But I have absolutely no clue on how to do this :?

Re: Create a unique id and add that to mysql database

Posted: Tue Feb 22, 2011 5:31 pm
by izeqb
Okay... after some searching and messing around, I stumbled upon base_convert :)

So, this is how I did it:

$letters = "aaaa";
$numbers = base_convert($letters, 36, 10);
$newnumber = $numbers+1;
$newletter = base_convert($newnumber, 10, 36);
echo $newletter;

will return: aaab

I know this is a no-brainer for a lot of you in here... but just wanted to share anyway, just in case :)