PHP / MySQL formatingng:如何使用此类数据的示例?

发布于 2024-08-18 05:30:46 字数 445 浏览 10 评论 0原文

对于这种奇怪/模糊的问题提前表示歉意:)。我已经多次遇到过这种存储在 MySQL 数据库中的格式,我想知道这些数据是如何使用的?例如,这行代码来自 bbPress 论坛插件 bb_message。

a:8:{s:9:"max_inbox";i:50;s:13:"auto_add_link";b:1;s:9:"email_new";b:1;s:11:"email_reply";b :1;s:9: "email_add";b:1;s:13:"email_message";b:0;s:16:"threads_per_page";i:0;s:7:"version";s:3:"1.0";}

什么是字符和字母还好吗?例如,我猜测 s:9:"max_inbox" 指的是一个包含九个字符的字符串,该字符串是 max_inbox。但是,当从数据库中提取这些数据时,如何使用 PHP 对其进行操作(以及为什么需要 s:9)?

谢谢大家!

Sorry in advance for the kind of odd/vague question :). I've come across this kind of formatting stored in MySQL databases on several occasions now and I'm wondering how the data is used? For example, this line of code was from the bbPress forums plugin bb_message.

a:8:{s:9:"max_inbox";i:50;s:13:"auto_add_link";b:1;s:9:"email_new";b:1;s:11:"email_reply";b:1;s:9:
"email_add";b:1;s:13:"email_message";b:0;s:16:"threads_per_page";i:0;s:7:"version";s:3:"1.0";}

What's up with the characters and letters? For example I'm guessing s:9:"max_inbox" refers to a string of nine characters and the string is max_inbox. But how is this data manipulated with PHP (and why would the s:9 be necessary) when its pulled from the database?

Thanks all!

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

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

发布评论

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

评论(2

游魂 2024-08-25 05:30:46

我相信这是由 PHP 的序列化函数生成的字符串。此函数使您能够将(复杂)对象或数组表示为字符串。

此外,我认为您所引用的数字可能需要使标记化(不确定这是否是适当的术语)将字符串返回到其相关部分更容易。没有经过太多考虑,我认为它是为了避免标记生成器可能因包含分隔符(:;”)的序列化对象的实际值而阻塞的情况。

I believe this is a string that is generated by PHP's serialize function. This function enables you to represent a (complex) object or array as a string.

Furthermore I think the numbers you are refering to are probably needed to make tokenizing (not sure if that is the appropriate term here) the string back to it's relevant parts easier. Without having given it much thought, I presume it is meant to circumvent situations where the tokenizer might choke on actual values of the serialized object that contain delimiters (:;").

梦境 2024-08-25 05:30:46

使用 PHP 的 unserialize 函数来解码字符串。

反序列化 PHP 文档

这个字符串解码为这个数组,如下所示:

php > $f='a:8:{s:9:"max_inbox";i:50;s:13:"auto_add_link";b:1;s:9:"email_new";b:1;s:11:"email_reply";b:1;s:9:"email_add";b:1;s:13:"email_message";b:0;s:16:"threads_per_page";i:0;s:7:"version";s:3:"1.0";}';
php > var_dump(unserialize($f));
array(8) {
["max_inbox"]=>
int(50)
["auto_add_link"]=>
bool(true)
["email_new"]=>
bool(true)
["email_reply"]=>
bool(true)
["email_add"]=>
bool(true)
["email_message"]=>
bool(false)
["threads_per_page"]=>
int(0)
["version"]=>
string(3) "1.0"
}

原因像这样存储数据的原因是您可以在一个数据库字段中存储多个参数。

存储序列化数组使您不必担心数据库的架构。例如,Friendfeed 使用 MySQL 来实现此目的,它也类似于 MongoDB 等“NoSQL”存储系统的风格。我自己更喜欢使用 JSON 而不是 PHP 序列化。

Use PHP's unserialize function to decode the strings like that.

unserialize PHP docs

This string decodes to this array like so:

php > $f='a:8:{s:9:"max_inbox";i:50;s:13:"auto_add_link";b:1;s:9:"email_new";b:1;s:11:"email_reply";b:1;s:9:"email_add";b:1;s:13:"email_message";b:0;s:16:"threads_per_page";i:0;s:7:"version";s:3:"1.0";}';
php > var_dump(unserialize($f));
array(8) {
["max_inbox"]=>
int(50)
["auto_add_link"]=>
bool(true)
["email_new"]=>
bool(true)
["email_reply"]=>
bool(true)
["email_add"]=>
bool(true)
["email_message"]=>
bool(false)
["threads_per_page"]=>
int(0)
["version"]=>
string(3) "1.0"
}

The reason for storing data like this is that you can store multiple parameters in one database field.

Storing serialized arrays allows you to not worry about the schema of your database. Friendfeed does this with MySQL for instance, and it's also similar to the style of the 'NoSQL' storage systems like MongoDB. I prefer to use JSON over PHP serialize, myself.

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