重复一个函数
这是我在此处发布的问题的后续问题。
我使用以下代码为用户提供唯一的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
超级简单。
你有3个功能。
一 - 主要生成guid函数。
二 - 子生成guid函数。
三-子插入引导功能。
子生成函数:
是否实际生成,返回一个guid。
子插入功能:
尝试将 guid 插入数据库。失败时返回 false/null。真实/成功数据。
主要功能:
循环中:
调用子函数来获取 guid。
调用子函数插入数据库,其中 guid 是唯一键或主键。
失败时,重新启动循环。
成功退出循环并返回guid。
例子:
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:
想想这个。
Think about this.
我必须升级/修复一个与您的编码方式类似的系统,我必须告诉您,我真的开始讨厌原来的程序员了。
想一想您正在做什么,如果另一个用户在您询问 guid 是否有效(并返回“是”)和您实际插入行之间创建了一个帐户,那么您仍然可能会发生冲突。这太可怕了……
但是开发数据库程序的人想到了这一点,并为您提供了一个轻松的替代方案:标识列。它们提供了 3 个主要优点:
要使用它们,请将您的 ID 列作为身份列,当您执行插入时,请像这样编写:
然后您会返回为您生成的用户 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:
To use them, make your ID column to be an identity column, and when you do your insert, write it like this:
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.
我之前构建过类似的代码,我通常从一个 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
可能是,一种稍微不同的方法,
我会将生成逻辑移动到存储过程中,并要求数据库生成像 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.