English 中文(简体)
检查独一无二的粗金字?
原标题:Logical thing to do about checking for unique id?
  • 时间:2010-07-27 15:24:17
  •  标签:
  • php
  • mysql

我有随机生成的链接,长8位数,每位数50个数值,这意味着4x10^13可能的组合(我想)。 我每天大约有10个问题。

我的问题是,我是否应当检查每张重复问询表4,还是问它如何? 或使其10位数达到一定比例?

编辑:

我(可能复制)发电机

// START Generates Random String
function genRandString($len=8){
$base= ABCDEFGHKLMNPQRSTWXYZabcdefghjkmnpqrstwxyz23456789 ;
$max=strlen($base)-1;
$randstring1 =  ;
mt_srand((double)microtime()*1000000);
while (strlen($randstring1)<$len+1)
$randstring1.=$base{mt_rand(0,$max)};
return $randstring1;
}
// END Generates Random String
最佳回答

它取决于 p多米发电机的质量。 你可能不够远大,因此你更可能发生你所意识到的碰撞。

是否有理由使用UUID(? 这似乎与为此目的设计的最佳解决办法一样。

无论如何,我不建议在插入之前检查重复。 这取决于种族条件,即,在你检查之后,有人可以插入重复价值,但在你插入之前。 因此,你必须处理重大违约例外情况。 最好只尝试(不首先检查)并视需要处理例外情况。


您的评论和算法: 我不会使用这一洗衣计划。 http://en.wikipedia.org/wiki/ Self-information"rel=“nofollow noretinger”>information,分四位数的50种不同的数值。 因此,,一旦你在你的数据库中拥有几千个浏览点,碰撞的深度就相当大。

如何解决这一问题: 使用单质的提高主要价值,例如,AUTO_INCREMENT。 将这一数字改为字母数字示意图,使用base_convert():

$id = 12345678;
$str = base_convert($id, 10, 36);
echo "$str
";

结果是7clzi

如果你担心像1、1、0、0、o等信件中的混淆,你可以做一些习俗替代:

$from = array( 1 ,  l ,  i ,  0 ,  o );
$to   = array( A ,  B ,  C ,  D ,  E );
$str = str_replace($from, $to, $str);

现在该数值<代码>12345678改为7cBzC。 如果有人要求使用该法典的网页,则改换成:

$code = str_replace($to, $from, $code);
$id = base_convert($code, 36, 10); 
问题回答

预设一个时间档和抽查





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签