Jump to content

[SOLVED] generate unique number plus texts for each record


jkkenzie

Recommended Posts

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;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.