重复一个函数

发布于 2024-10-09 01:52:44 字数 1319 浏览 0 评论 0原文

这是我在此处发布的问题的后续问题。

我使用以下代码为用户提供唯一的 id:

function NewGuid() { 
    $s = strtoupper(uniqid(rand(),true)); 
    $guidText = substr($s,0,8) . '-' . substr($s,8,4) . '-' . substr($s,12,4). '-' . substr($s,16,4). '-' . substr($s,20); return $guidText; 
} 

$Guid = NewGuid();
echo $Guid;

$alphabet = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';

function base_encode($num, $alphabet) { 
    $base_count = strlen($alphabet); 
    $encoded = '';

    while ($num >= $base_count) {
        $div = $num/$base_count;
        $mod = ($num-($base_count*intval($div)));
        $encoded = $alphabet[$mod] . $encoded;
        $num = intval($div);
    }

    if ($num) $encoded = $alphabet[$num] . $encoded;
    return $encoded;    
}

function base_decode($num, $alphabet) { 
    $decoded = 0;
    $multi = 1;

    while (strlen($num) > 0) {
        $digit = $num[strlen($num)-1];
        $decoded += $multi * strpos($alphabet, $digit);
        $multi = $multi * strlen($alphabet);
        $num = substr($num, 0, -1);
    }

    return $decoded;
}

echo base_encode($Guid, $alphabet);

因此,如果生成的 id 已在使用中,我希望此代码能够重复,直到生成全新的 id 并将其输入数据库。另外,我是编程新手,所以请随意详细说明;)

有什么想法吗?谢谢。

This is a follow-up question to a question I posted here.

I am using the following code to give users a unique id:

function NewGuid() { 
    $s = strtoupper(uniqid(rand(),true)); 
    $guidText = substr($s,0,8) . '-' . substr($s,8,4) . '-' . substr($s,12,4). '-' . substr($s,16,4). '-' . substr($s,20); return $guidText; 
} 

$Guid = NewGuid();
echo $Guid;

$alphabet = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';

function base_encode($num, $alphabet) { 
    $base_count = strlen($alphabet); 
    $encoded = '';

    while ($num >= $base_count) {
        $div = $num/$base_count;
        $mod = ($num-($base_count*intval($div)));
        $encoded = $alphabet[$mod] . $encoded;
        $num = intval($div);
    }

    if ($num) $encoded = $alphabet[$num] . $encoded;
    return $encoded;    
}

function base_decode($num, $alphabet) { 
    $decoded = 0;
    $multi = 1;

    while (strlen($num) > 0) {
        $digit = $num[strlen($num)-1];
        $decoded += $multi * strpos($alphabet, $digit);
        $multi = $multi * strlen($alphabet);
        $num = substr($num, 0, -1);
    }

    return $decoded;
}

echo base_encode($Guid, $alphabet);

So, if a id that is generated is already in use, i want this code to be able to repeat until a completely new id is generated and entered into the database. IN addition i am new to programming, so please feel free to elaborate ;)

Any thoughts? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梨涡 2024-10-16 01:52:44

超级简单。

你有3个功能。

一 - 主要生成guid函数。
二 - 子生成guid函数。
三-子插入引导功能。

子生成函数:
是否实际生成,返回一个guid。

子插入功能:
尝试将 guid 插入数据库。失败时返回 false/null。真实/成功数据。

主要功能:
循环中:
调用子函数来获取 guid。
调用子函数插入数据库,其中 guid 是唯一键或主键。
失败时,重新启动循环。
成功退出循环并返回guid。

例子:

function GetGuid()
{
    do
    {
        $guid = GenerateGuid();
    } while (!InsertGuid($guid));
    return $guid;
}

function GenerateGuid()
{
    // do your stuff here.
    return $guid;
}

function InsertGuid($guid)
{
    $sql = 'INSERT INTO `guid_table` (`guid`) VALUES ('$guid')';
    $connection = //do sql connect here;
    return // do sql execution here which will return false/null on failure;
}

Super easy.

You have 3 functions.

One - main generate guid function.
Two - Sub generate guid function.
Three - Sub insert guid function.

Sub generate function:
Does the actual generation, returns a guid.

Sub insert function:
Attempts to insert guid into the database. Returns false/null on failure. True/data on success.

Main function:
In a loop:
Call subfunction to get a guid.
Call subfunction to insert in the database where the guid is a unique or primary key.
On failure, restart the loop.
Success exits the loop and returns the guid.

Example:

function GetGuid()
{
    do
    {
        $guid = GenerateGuid();
    } while (!InsertGuid($guid));
    return $guid;
}

function GenerateGuid()
{
    // do your stuff here.
    return $guid;
}

function InsertGuid($guid)
{
    $sql = 'INSERT INTO `guid_table` (`guid`) VALUES ('$guid')';
    $connection = //do sql connect here;
    return // do sql execution here which will return false/null on failure;
}
撩人痒 2024-10-16 01:52:44
function randr($j = 8){
$string = "";
    for($i=0;$i < $j;$i++){
        srand((double)microtime()*1234567);
        $x = mt_rand(0,2);
        switch($x){
            case 0:$string.= chr(mt_rand(97,122));break;
            case 1:$string.= chr(mt_rand(65,90));break;
            case 2:$string.= chr(mt_rand(48,57));break;
        }
    }
return $string; 
}

    do{
    $id = randr();
    $check = mysql_query("SELECT uniq FROM users WHERE uniq = '$id'");
    }while(mysql_num_rows($check) != 0);

想想这个。

function randr($j = 8){
$string = "";
    for($i=0;$i < $j;$i++){
        srand((double)microtime()*1234567);
        $x = mt_rand(0,2);
        switch($x){
            case 0:$string.= chr(mt_rand(97,122));break;
            case 1:$string.= chr(mt_rand(65,90));break;
            case 2:$string.= chr(mt_rand(48,57));break;
        }
    }
return $string; 
}

    do{
    $id = randr();
    $check = mysql_query("SELECT uniq FROM users WHERE uniq = '$id'");
    }while(mysql_num_rows($check) != 0);

Think about this.

阳光下的泡沫是彩色的 2024-10-16 01:52:44

我必须升级/修复一个与您的编码方式类似的系统,我必须告诉您,我真的开始讨厌原来的程序员了。

想一想您正在做什么,如果另一个用户在您询问 guid 是否有效(并返回“是”)和您实际插入行之间创建了一个帐户,那么您仍然可能会发生冲突。这太可怕了……

但是开发数据库程序的人想到了这一点,并为您提供了一个轻松的替代方案:标识列。它们提供了 3 个主要优点:

  • 保证唯一(否则插入失败)
  • 它们自动成为主键
  • 数据库为您生成它们

要使用它们,请将您的 ID 列作为身份列,当您执行插入时,请像这样编写:

insert into users (name, passwordhash, extrastuff) 
values (@name, @passwordhash, @extrastuff);
select @@identity -- or ident_current() for mysql

然后您会返回为您生成的用户 ID 以供进一步处理。无论有多少用户同时尝试,这一切都是同时完成的。

I have to upgrade/fix a system that's written similarly to the way you code, and I gotta tell you, I've really grown to hate the original programmer.

Think about what you're doing for a sec, if another user creates an account between when you ask if the guid is valid (and get back yes) and when you actually insert the row, you can still have collisions. That's beyond horrible...

But the guys that made the database programs thought of this and offer you a painless alternative: identity columns. They provide 3 major advantages:

  • guaranteed to be unique (or the insertion fails)
  • they're automatically made into a primary key
  • the database generates them for you

To use them, make your ID column to be an identity column, and when you do your insert, write it like this:

insert into users (name, passwordhash, extrastuff) 
values (@name, @passwordhash, @extrastuff);
select @@identity -- or ident_current() for mysql

And you get back the user id generated for you for further processing. This is all done simultaneously, no matter how many users are trying this at the same time.

二货你真萌 2024-10-16 01:52:44

我之前构建过类似的代码,我通常从一个 null 变量开始,并且在 while ($variable == null) { } 内执行 while ($variable == null) { }

,我创建新代码,根据数据库检查它,如果它还不存在,我设置 $variable = $new_code (这将打破 while),然后进行相应的操作。

此外,我记录了可能发生的任何碰撞

希望这有帮助

I built similar code before, I typically start with a null variable, and I do while ($variable == null) { }

inside the while, I create the new code check it against the database and if it doesn't exist yet, I set $variable = $new_code (which will break the while) and then proceed accordingly.

Additionally, I log any collisions that may occur

hope this helps

兔小萌 2024-10-16 01:52:44

可能是,一种稍微不同的方法,

我会将生成逻辑移动到存储过程中,并要求数据库生成像 Oracle 中那样的唯一序列。

这样,我就可以避免多次前往检查号码是否唯一。

May be , a slight different approach

i would move my generation logic inside a stored procedure and ask database to generate a unique sequence like in oracle.

This way , i would be able to avoid numerous trips to check the number is unique or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文