PHP/MySQL:存储和检索UUIDS [英] PHP/MySQL: Storing and retrieving UUIDS
问题描述
我正在尝试将UUID添加到几个表中,但是我不确定存储/检索这些表的最佳方法是什么.我知道使用BINARY(16)而不是VARCHAR(36)效率更高.经过一些研究,我还发现您可以使用以下方法将UUID字符串转换为二进制:
I'm trying to add UUIDs to a couple of tables, but I'm not sure what the best way to store/retrieve these would be. I understand it's far more efficient to use BINARY(16) instead of VARCHAR(36). After doing a bit of research, I also found that you can convert a UUID string to binary with:
UNHEX(REPLACE(UUID(),'-',''))
请原谅我的无知,但是有没有一种简便的方法可以使用PHP,然后在需要时将其转换回字符串以提高可读性?
Pardon my ignorance, but is there an easy way to this with PHP and then turn it back to a string, when needed, for readability?
此外,如果我将其用作主键而不是auto_increment,会有所不同吗?
Also, would it make much difference if I used this as a primary key instead of auto_increment?
找到答案的一部分:
$bin = pack("h*", str_replace('-', '', $guid));
您将如何打开包装?
推荐答案
好的-尝试回答我自己的问题.这是我能想到的最好的方法:
Okay -- going to try to answer my own question. This is the best I could come up with:
包装:
$binary = pack("h*", str_replace('-', '', $string));
解包
$string = unpack("h*", $binary);
$string = preg_replace("/([0-9a-f]{8})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{12})/", "$1-$2-$3-$4-$5", $string);
任何人都可以看到这个问题吗?
Is there any problem with this anyone can see?
这篇关于PHP/MySQL:存储和检索UUIDS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!