简体   繁体   English

使用PHP和MYSQL生成唯一ID

[英]Generate a Unique ID using PHP and MYSQL

Hi I am creating a system that processes and ID and a UID, The UID we are generating randomly but I am a little stuck, I need to always generate a UID that does not currently exist in the db as the field is a unique field used on the front end so as not to expose the real ID. 嗨,我正在创建一个处理进程,ID和UID的系统,我们正在随机生成的UID,但是有点卡住了,我需要始终生成db中当前不存在的UID,因为该字段是使用的唯一字段在前端,以免暴露真实ID。

So to recap, I am trying to generate a unique id that does not currently exist in the DB the part I haven't got working is the cross checking in the db so it sometimes will give a number that already exists in the db even though it shouldn't thanks in advance. 因此,回顾一下,我正在尝试生成数据库中当前不存在的唯一ID,而我尚未使用的部分是数据库中的交叉检查,因此有时即使数据库中已经存在一个数字,它也会给出一个不应该事先感谢。

This is my code so far: 到目前为止,这是我的代码:

function uniqueID($table) 
{
$db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

$possible = '1234567890';
$code = '';
$characters = mt_rand(7,14);
$i = 0;

while($i < $characters) 
{ 

    $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
    $i++;

}

$result = $db->query('
SELECT uniqueID
FROM '.$table.'
WHERE uniqueID = "'.$code.'"
LIMIT 1
');

$totalRows = $result->num_rows;

if(!$result)
{

    return $db->error;

}
else
{

    if($totalRows > 0)
    {

        return uniqueID($table);

    }
    else
    {

        return $code;

    }

}

}

To generate unic UID you can use time, i think it was a very small chanse that records will be added in the same second, with two random data. 要生成unic UID,您可以使用时间,我认为这是一个很小的改变,可以在同一秒内添加带有两个随机数据的记录。

write some function which return it to you like that 编写一些函数将其返回给您

function generate_uid(){
 return md5(mktime()."-".rand()."-".rand());
}

In PHP there's a function called uniqid() 在PHP中,有一个名为uniqid()的函数

http://php.net/manual/en/function.uniqid.php http://php.net/manual/zh/function.uniqid.php

I could talk about generating ids, like the others did, but this is not your question. 我可以像其他人一样谈论生成id的问题,但这不是您的问题。 Your query seems fine. 您的查询似乎很好。 If it returns 0 rows but you seem to find the code in the database, then most likely it only looks the same, but actually isn't. 如果它返回0行,但是您似乎在数据库中找到了代码,则很可能它看起来只是一样,但实际上不是。 It could be padded by whitespace. 可以用空格填充。

One way to solve this is by selecting the last row of your user database and have your script to check for the id field (you can achieve this by performing a select ordering by ID in descendent mode) then you can use that info for randomize numbers greater than that ID. 解决此问题的一种方法是选择用户数据库的最后一行,并让脚本检查ID字段(您可以通过在后代模式中按ID执行选择排序来实现此目的),然后可以使用该信息来随机化数字大于该ID。

EDIT 编辑

$result = $db->query('
SELECT uniqueID
FROM '.$table.'
');

$already_in_database = array();
while ($row = mysql_fetch_array($result)){
    $already_in_database[] = $row['UID'];    
}

$new = rand(0,$some_max_value);
while(in_array($new,$already_in_database)){
    $new = rand(0,$some_max_value);
}

I figured out what the problem was already, As I mentioned to everyone the code generation was not the issue! 我已经弄清楚了问题所在,正如我对所有人所说,代码生成不是问题! The issue was that the cross check was not working correctly. 问题是交叉检查无法正常工作。 So all I did was removed this loop 所以我所做的一切都被删除了

while($i < $characters) 
{ 

    $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
    $i++;

}

As this was causing my unique ID to end up wrong. 因为这导致我的唯一ID最终错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM