简体   繁体   English

如何访问WordPress插件变量/ MySQL数据?

[英]How to access WordPress Plugin Variable/MySQL Data?

I have a plugin that is an extension to woocommerce. 我有一个插件,它是woocommerce的扩展。 This plugin stores information under the MySQL Table "wp_woocommerce_order_itemmeta". 该插件将信息存储在MySQL表“ wp_woocommerce_order_itemmeta”下。 Under the column "meta_key", there is "_wc_deposit_meta". 在“ meta_key”列下,有“ _wc_deposit_meta”。 Next to the column "meta_key" I have "meta_value". 在“ meta_key”列旁边,我有“ meta_value”。 Under the "meta_value" you find a:5:{s:6:"enable";s:3:"yes";s:7:"deposit";d:197.67000000000001591615728102624416351318359375;s:9:"remaining";d:401.32999999999998408384271897375583648681640625;s:5:"total";d:599;s:5:"ratio";d:1;} . 在“ meta_value”下,您可以找到a:5:{s:6:"enable";s:3:"yes";s:7:"deposit";d:197.67000000000001591615728102624416351318359375;s:9:"remaining";d:401.32999999999998408384271897375583648681640625;s:5:"total";d:599;s:5:"ratio";d:1;} While learning PHP I have never came across a value like this. 在学习PHP时,我从未遇到过这样的价值。 Could someone explain what this value is and how do I access the value for "deposit". 有人可以解释这个值是什么,以及我如何访问“存款”的值。 Is this just another example of a multidimensional array? 这仅仅是多维数组的另一个示例吗?

I have been trying to access this value by $my_order = $order->get_items(); 我一直试图通过$my_order = $order->get_items();访问此值$my_order = $order->get_items(); in my functions.php, which is returning an array. 在我的functions.php中,它返回一个数组。 I see the correct array called "_wc_deposit_meta" with this weird value that I do not know how to access because of the commas and colons. 我看到带有这个奇怪值的正确数组“ _wc_deposit_meta”,由于逗号和冒号,我不知道该如何访问。

Sorry for the noob question but I have been trying to research this buy could not find a simple black and white answer. 很抱歉菜鸟问题,但我一直在尝试研究此购买交易,找不到简单的黑白答案。 If someone could point me in the right direction, I would be very appreciative. 如果有人能指出正确的方向,我将不胜感激。

The value that you're looking at has been serialized() and stored in the database. 您正在查看的值已被serialized()并存储在数据库中。 Simply take that variable and use unserialize() on it, like so: 只需获取该变量并对其使用unserialize() ,如下所示:

$myVal = unserialize($databaseColumn);
  1. serialize 连载
  2. unserialize 反序列化

When WordPress needs to store multi-dimensional data into a field it applies PHP's serialize() function to convert the data into a storable string, then unserialize() when reading the value to return it to it's proper data structure. 当WordPress需要将多维数据存储到一个字段中时,它将应用PHP的serialize()函数将数据转换为可存储的字符串,然后在读取值时将unserialize()转换为正确的数据结构。

The anatomy of serialized data definitions are described in the first comment on PHP's serialize() documentation: 在PHP的serialize()文档的第一条评论中描述了序列化数据定义的剖析:

  • String: s:[size]:[value]; 字符串: s:[size]:[value];
  • Integer: i:[value]; 整数: i:[value];
  • Double: d:[value]; 双精度: d:[value];
  • Boolean: b:[value]; 布尔值: b:[value]; (does not store "true" or "false", does store '1' or '0') (不存储“ true”或“ false”,但存储“ 1”或“ 0”)
  • Null: N; 空: N;
  • Array: a:[size]:{[key definition];[value definition];(repeated per element)} 数组: a:[size]:{[key definition];[value definition];(repeated per element)}
  • Object: O:[strlen(object name)]:[object name]:[object size]:{s:[strlen(property name)]:[property name]:[property definition];(repeated per property)} 对象: O:[strlen(object name)]:[object name]:[object size]:{s:[strlen(property name)]:[property name]:[property definition];(repeated per property)}

More specifically, WordPress uses it's own maybe_serialize() and maybe_unserialize() wrappers to mitigate the chance of (un)serializing when not necessary. 更具体地说,WordPress使用它自己的maybe_serialize()maybe_unserialize()包装器来减轻不必要时进行(非)序列化的机会。

In the particular serialized data you are working with, your deposit is represented by the key/value definitions s:7:"deposit";d:197.67000000000001591615728102624416351318359375; 在您正在使用的特定序列化数据中,您的存款由键/值定义s:7:"deposit";d:197.67000000000001591615728102624416351318359375; . To alter your deposit value by hand, you would modify the double after d: . 要手动更改您的存款价值,您可以在d:之后修改double。 To modify the value programmatically in WordPress, use the Metadata API to query the field using a "get" function (which will implicitly unserialize it), modify the value of the 'deposit' field in the returned associative array, then save the new value using an "update" function from the Metadata API. 要在WordPress中以编程方式修改值,请使用Metadata API使用“获取”函数(将隐式反序列化)查询字段,修改返回的关联数组中'deposit'字段的值,然后保存新值使用元数据API中的“更新”功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM