Jump to content
mds1256

How to generate unique IDs that only contains alphanumeric characters

Recommended Posts

18 minutes ago, requinix said:

I've already told you what I think the better way is.

You did? I missed it then, I think. 

Share this post


Link to post
Share on other sites

Use sessions.

Share this post


Link to post
Share on other sites

How do I use a session to create a unique Id for my user so that I have a unique row in the table for them. Wouldn't sessions create a new id everytime a new session started? I just realized this may be more of a MySQL question than a php one, I need a way to generate GUIDs for my db keys

Share this post


Link to post
Share on other sites

Don't your users create their own login with a unique user ID at registration time?

Share this post


Link to post
Share on other sites
1 hour ago, gw1500se said:

Don't your users create their own login with a unique user ID at registration time?

They have a username, yes. But I need a key value as well that is used to identity the specific user in other tables.

Share this post


Link to post
Share on other sites

Why can't the user name be the key value?

Share this post


Link to post
Share on other sites

Usernames can be changed (unless I disallow that) and their lengths can vary. For OCD purposes I was planning on 32 bit strings.

Share this post


Link to post
Share on other sites
3 hours ago, Karaethon said:

They have a username, yes. But I need a key value as well that is used to identity the specific user in other tables.

That would be the user_id that you should have in the table. This is most commonly an auto-increment column.

Share this post


Link to post
Share on other sites

Ah, an auto-increment. Why TF didn't I think of that. [Initiate headslap]

I guess I don't "need" random identifiers, consecutive ones work too.

Share this post


Link to post
Share on other sites
18 hours ago, Karaethon said:

Ah, an auto-increment. Why TF didn't I think of that. [Initiate headslap]

I guess I don't "need" random identifiers, consecutive ones work too.

Integers are the most obvious and performant solution to primary keys in small to mid size systems where you can have a single database.  MySQL makes it easy with AUTO_INCREMENT.  Make sure that you always define your columns as UNSIGNED so you don't waste half your key space.

Assuming MySQL, in most cases the INT type is perfect, using 4 bytes and supporting up to 4,294,967,295 keys.

If you have other support tables, you can save space (and increase overall performance) by using tinyint, smallint or mediumint types.  The more compact your db the better it will perform over time (assuming proper indexing of required values).  You do not need to add indexes for primary key or key columns.  Also, make sure you used INNODB for all your tables.

 

Share this post


Link to post
Share on other sites

Ok so this DB has 4 (I expect no need for more...) tables: Players, Vaults, Guesses, and Purchases. Relationally the Vaults and Players tables are linked to the other two. PlayerID and VaultID are referenced in Guesses, and PlyerID is referenced in Purchases. When a player wins a vault his PlayerID is linked to that VaultID in a winner field. 

I believe I would only need unique ID keys for player and vault, so small int for player and int for vault? There could potentially be many thousands of Vaults after a time, although I expect to be able to dump old Vaults after a period, maybe 3 years. (IRS record keeping)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.