Create a unique id and add that to mysql database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
izeqb
Forum Newbie
Posts: 4
Joined: Sun Dec 19, 2010 7:23 am

Create a unique id and add that to mysql database

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

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

Post by John Cartwright »

Why not create the row in the db, get the id using mysql_insert_id(), process whatever, then update the record?
izeqb
Forum Newbie
Posts: 4
Joined: Sun Dec 19, 2010 7:23 am

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

Post 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 :?
izeqb
Forum Newbie
Posts: 4
Joined: Sun Dec 19, 2010 7:23 am

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

Post 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 :)
Post Reply