Jump to content

Recommended Posts

Hi!,

I have a database of about 10,000 records. I would like to generate a membership number for each record.

 

I would like to mix text and numbers in my generated membership number.

 

What is the best way and functions to use.

 

thanks in advance.

Joe

a quick and dirty way would be to create a md5 hash of the results of rand() in mysql...

 

SELECT MD5(RAND());

 

and then you would have to check to make sure it doesn't already exist, even though it would be highly unlikely...

 

maybe a better solution would to base the id off of existing data of each member, to make it a bit more meaningful and not completely random... and maybe mix it with a bit of randomness with rand()...

 

just rambling, hope this helps...

What about using hexdecimal numbers and increasing each record hexnumber by one? Then you don't have to check if the number doesn't already exists.

 

The reason for using letters as wel as numbers as id's is usually to shorten the length of the number of characters and not to just make it look cool.

Here's a function that will generate a random string of whatever size you'd want.  You of course have to check the DB to see if your random string already exists for another member before setting it.

 

function rand_gen($size)
{
$chars = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789";

$rand_str = "";

for($i = 0; $i < $size; $i++)
{
	$rand_str .= substr($chars, rand(0, 61), 1);
}

return $rand_str;
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.