Jump to content

Unique ID's


The Little Guy

Recommended Posts

What would be the best way to create a unique and random 10 digit number that isn't in a mysql database, without querying the database many times?

 

I know 10 is a large enough number so it probably will be hard to have to query the db more than once to see if the number already exists, but I believe that means I can have: 10,000,000,000 unique numbers but say 98% of those are taken, which means there are 980,000,000 numbers left. Which means I would have a greater chance of having to query the database more that once or twice.

 

How can I minimize the number of times I have to query the database? I would also like to be able to do this with a 32 alpha numeric string, and one that is about 50 alpha numeric characters (with dashes).

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/187665-unique-ids/
Share on other sites

Well in my opinion you can never have a "randomly" generated number that won't in some way have the chance of being duplicated over a large period of time. Why don't you just have a function to say make an md5 of a unique string, depending on what you're doing this could be a user name or file name or whatever, then replace all of the letters with their corresponding numbers, then substr the first 10?

Link to comment
https://forums.phpfreaks.com/topic/187665-unique-ids/#findComment-990763
Share on other sites

Actually I did think of one more way....

if you create all accounts beforehand

u can have a status field, where ya can set it as in use or not in use

with a query, you can retirve how many accts are not in use

with this, you can generate a number between 1 and not in use accts

with another query u can fetch the id

dummy code

$arr=mysql_fetch_row(mysql_query("SELECT Count(id) FROM users WHERE status='not in use'));
$not_in_user=$arr[0];

$newid=rand(1,$not_in_use);

$arr=mysql_fetch_assoc(mysql_query('SELECT * FROM users WHERE status='not in use' AND LIMIT=1 OFFSET=$newid"));

$id=$arr['id']

 

That would be the simplest, but as said, all accounts must be already in the database....

Hopefully above dummy code is some help

Link to comment
https://forums.phpfreaks.com/topic/187665-unique-ids/#findComment-990776
Share on other sites

I came up with this function:

 

Any thoughts?

 

function randomID($len = 10){
$str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$tid = md5(uniqid().time());
$len = strlen($tid);
$id = '';
$tmp = '';
for($i = 0;$i < $len; $i++){
	$rand = round(rand(0, 1));
	$pos = strpos($str, $tid{$i});
	if($rand == 0 && ctype_alpha($tid{$i})){
		$tmp .= $str{$pos + 26};
	}else{
		$tmp .= $tid{$i};
	}
}
for($i = 0;$i < $len; $i++){
	if(!ctype_digit($tid{$i})){
		$id .= strpos($str, $tid{$i});
	}else{
		$id .= $tid{$i};
	}
}
return substr($id, 0, $len);
}

Link to comment
https://forums.phpfreaks.com/topic/187665-unique-ids/#findComment-990777
Share on other sites

... then replace all of the letters with their corresponding numbers...

 

How do you replace letters with corresponding numbers? Or should I say how do you know what the corresponding number is to a letter?

 

err...

 

$letters = range('a','z');
$numbers = range('1','26');
$string = str_ireplace($letters,$numbers,$string);

Link to comment
https://forums.phpfreaks.com/topic/187665-unique-ids/#findComment-990778
Share on other sites

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.