如何使用PHP和MySQL基于当前表行生成随机唯一ID?

时间:2021-09-10 08:28:06

I have a table that has an auto-increment "id". I have another column in the same table that has a "key", which I want to be based on the auto-increment "id". This will ensure that there is never a duplicate "key". I want to preferably have a 6 character unique id generator. I found this online, but I am open to other suggestions.

我有一个具有自动增量“id”的表。我在同一个表中有另一列有一个“键”,我希望它基于自动增量“id”。这将确保永远不会有重复的“密钥”。我希望最好有一个6个字符的唯一id生成器。我在网上找到了这个,但我对其他建议持开放态度。

2 个解决方案

#1


0  

echo $result['id'] . '_' . uniqid('', true);

this will produce something like 1_513c5858c04967.71142475, the prefix ID_ will be always unique based on the database ofcourse

这将产生类似于1_513c5858c04967.71142475的东西,前缀ID_将始终是唯一的基于数据库的课程

isnt it unique enough?

它不够独特吗?

ahh you want 6 characters, what about:

啊,你想要6个字符,那么:

function randString($length, $charset='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
{
    $str = '';
    $count = strlen($charset);
    while ($length--) {
        $str .= $charset[mt_rand(0, $count-1)];
    }

    return $str;
}

  $key = $result['id'] . '_' . randString(6); //this is unique, cause even you get at a chance of a million that you will get 2 same randString(6); you will have always unique prefix ID_ so you will have 1_G7owOq and 2_G7owOq

  //the code below is just an example of how to loop column for unique key and u dont need it :P just maybe someone else needs it.
  while(mysqli_num_rows(mysqli_query($con, "SELECT id FROM table WHERE key = '".mysqli_real_escape_string($con, $key)."'")) > 0) { 
    $key = $result['id'] . '_' . randString(6); 
  }

#2


0  

Then there is the last way i posted you here:

然后我在这里发布你的最后一种方式:

$key = randString(6);
while(mysqli_num_rows(mysqli_query($con, "SELECT id FROM table WHERE key = '".mysqli_real_escape_string($con, $key)."'")) > 0) { 
    $key = randString(6); 
}

It will always create random 6 char key, because if it hits already existing key in db, it will re-create new one and so on, until its unique:] It is the best way for u, this way I'm also using :]

它将始终创建随机6个char键,因为如果它击中db中已存在的键,它将重新创建新的等等,直到它的唯一:]这是你的最佳方式,这样我也在使用:]

Actually the best-best way would be to generate table with all existing unique keys (62^6) * 6 bytes each key -> 317 GB if I'm not mistaken (morning) :] I dont think you will hit 1/10000 of that :]

实际上最好的方法是生成包含所有现有唯一键的表(62 ^ 6)*每个键6个字节 - > 317 GB如果我没有弄错(早上):]我不认为你会达到1/10000那个:]

#1


0  

echo $result['id'] . '_' . uniqid('', true);

this will produce something like 1_513c5858c04967.71142475, the prefix ID_ will be always unique based on the database ofcourse

这将产生类似于1_513c5858c04967.71142475的东西,前缀ID_将始终是唯一的基于数据库的课程

isnt it unique enough?

它不够独特吗?

ahh you want 6 characters, what about:

啊,你想要6个字符,那么:

function randString($length, $charset='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
{
    $str = '';
    $count = strlen($charset);
    while ($length--) {
        $str .= $charset[mt_rand(0, $count-1)];
    }

    return $str;
}

  $key = $result['id'] . '_' . randString(6); //this is unique, cause even you get at a chance of a million that you will get 2 same randString(6); you will have always unique prefix ID_ so you will have 1_G7owOq and 2_G7owOq

  //the code below is just an example of how to loop column for unique key and u dont need it :P just maybe someone else needs it.
  while(mysqli_num_rows(mysqli_query($con, "SELECT id FROM table WHERE key = '".mysqli_real_escape_string($con, $key)."'")) > 0) { 
    $key = $result['id'] . '_' . randString(6); 
  }

#2


0  

Then there is the last way i posted you here:

然后我在这里发布你的最后一种方式:

$key = randString(6);
while(mysqli_num_rows(mysqli_query($con, "SELECT id FROM table WHERE key = '".mysqli_real_escape_string($con, $key)."'")) > 0) { 
    $key = randString(6); 
}

It will always create random 6 char key, because if it hits already existing key in db, it will re-create new one and so on, until its unique:] It is the best way for u, this way I'm also using :]

它将始终创建随机6个char键,因为如果它击中db中已存在的键,它将重新创建新的等等,直到它的唯一:]这是你的最佳方式,这样我也在使用:]

Actually the best-best way would be to generate table with all existing unique keys (62^6) * 6 bytes each key -> 317 GB if I'm not mistaken (morning) :] I dont think you will hit 1/10000 of that :]

实际上最好的方法是生成包含所有现有唯一键的表(62 ^ 6)*每个键6个字节 - > 317 GB如果我没有弄错(早上):]我不认为你会达到1/10000那个:]