Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为故障代码生成随机但唯一的字母数字ID_Php_Mysqli - Fatal编程技术网

Php 为故障代码生成随机但唯一的字母数字ID

Php 为故障代码生成随机但唯一的字母数字ID,php,mysqli,Php,Mysqli,我想为我的web应用程序中的故障代码生成一个随机但唯一的ID,但是到目前为止,我还不能想出正确的解决方案,也不确定从哪里开始 我正在寻找一个解决方案,将使用字母和数字为例 故障代码:AF-5754 由于这些都与数据库中存储的错误有关,我需要它们是唯一的,数据库中的id只是一个数字 有什么可能的方法 function randomstring($len) { $string = ""; $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrst

我想为我的web应用程序中的故障代码生成一个随机但唯一的ID,但是到目前为止,我还不能想出正确的解决方案,也不确定从哪里开始

我正在寻找一个解决方案,将使用字母和数字为例

故障代码:AF-5754

由于这些都与数据库中存储的错误有关,我需要它们是唯一的,数据库中的id只是一个数字

有什么可能的方法

function randomstring($len)
{
$string = "";
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for($i=0;$i<$len;$i++)
$string.=substr($chars,rand(0,strlen($chars)),1);
return $string;
}
函数随机字符串($len)
{
$string=“”;
$chars=“abcdefghijklmnopqrstuvxyzabefghijklmnopqrstuvxyzo123456789”;

对于($i=0;$i为什么不从sql id自动生成的唯一代码生成一个人性化的代码

<?php
function getFrom($id) // Get a human-friendly code for unique id from database
{

$prefixes = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";  // Prefix posibilities

$primaryPrefx  = "A"; // Another prefix to increase id limit

if($id > 259999)
{
$primaryPrefx = $prefixes[intval($id / 2600000)];
$id = $id % 2600000;
}

$prefix = "";  // Prefix to be used
$errorCode = "";  // The integer to be added after the prefix

$prefix = $prefixes[intval($id / 100000)]; // get the prefix based on $id ( < 10000 => A , 10000 => B, 20000 => C, 30000 => D etc...) 


$id = $id % 100000; // now for the integer part (% returns remainder of division. This gets rid of > 9999 part)

if($id < 10000)
{
  $errorCode .= "0"; // We need five digits after prefix
if($id < 1000)
{
    $errorCode .= "0"; // We need five digits after prefix

    if($id < 100)
        {
            $errorCode .= "0"; // We need five digits after prefix
            if($id < 10)
            {
                $errorCode .= "0" . $id;                
            }
            else
                $errorCode .= $id;
        }
    else
        $errorCode .= $id;

}
else
    $errorCode .= $id;
}
else
    $errorCode = $id;

return $primaryPrefx . "" . $prefix . "-" . $errorCode;
}


 echo getFrom(1568110); // Gives AP-68110
 ?>

是的,我只是想从表中返回id号,但是我不确定返回AA-0232的安全性是否更安全,但是我可能错了。上面的代码可能是最大值26001是最大值。请再次阅读答案以获得解决方案(另一个前缀差不多)至于安全含义,$prefixes字符串可以是一个随机字符串(但每个字符都是唯一的)。您还可以在4个整数之间进行切换。当我复制并粘贴代码以测试itPHP时,代码会抛出一个错误。语法检查:解析错误:语法错误,意外“}“在你的代码第33行,它对我来说工作正常。我自己粘贴了一份副本,它工作正常。你能再试一次吗?先刷新这个页面,我做了一些编辑,也许你有一个过时的版本。”
function getIdFromCode($code)
{
   $prefixes = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   $id = 0;
   $id += strrpos($prefixes, $code[0]) * 260000; // first prefix
   $id += strrpos($prefixes, $code[1]) * 100000; // second prefix
   $id += intval(substr($code, 3)); // AA- is removed
   return $id;
}

echo getIdFromCode("AP-68110"); // Gives 1568110