2015-11-20 44 views
3

我必须在这里丢失一些非常简单的东西,但我无法弄清楚。我需要访问存储在数据库中的序列化数据。 在数据库中存储的值是无法访问序列化数据

a:2:{s:8:"last_tab";s:1:"1";s:11:"footer_text";s:13:"Default Text2";} 

我需要得到“默认文本2”。

在我的本地环境我附和

get_option('tt_options')['footer_text'] 

,并得到了我所需要的价值。

问题出现在我的临时环境中。在那里,当我输入

get_option('tt_options')['footer_text'] 

页面崩溃,虽然

get_option('tt_options') 

工作正常。

我能想到的我的地方,我的临时环境之间的唯一区别是,在数据库中的表使用不同的引擎:

临时环境: 引擎:MyISAM数据

本地环境: 引擎: InnoDB

顺便说一句,我使用WordPress,如果有帮助。

无论如何,我试图通过以下几种方式访问​​该值。已注释的行仅在我的分段环境中导致问题。我发现有趣的是,当我试图从没有存储在数据库中的序列化数据访问的价值,但在一个变量(见下文:

echo ($returnValue['footer_text']); 

),我得到我需要的价值。有没有人对导致问题的原因有所了解,以便指引我朝着正确的方向发展?提前感谢您的时间和指导。

$returnValue = unserialize('a:2:{s:8:"last_tab";s:1:"1";s:11:"footer_text";s:13:"Default Text2";}'); 

echo '0:: '; 
print_r($returnValue); 
echo "<hr>"; 

echo '1:: '; 
echo ($returnValue['footer_text']); 
echo "<hr>"; 

echo '2:: '; 
//echo get_option('tt_options')['footer_text']; 
echo "<hr>"; 

echo '3:: '; 
var_dump(get_option('tt_options')); 
echo "<hr>"; 

echo '4:: '; 
//var_dump(get_option('tt_options')['footer_text']); 
echo "<hr>"; 

echo '5:: '; 
echo get_option('tt_options'); 
echo "<hr>"; 

echo '6:: '; 
print_r(get_option('tt_options')); 
echo "<hr>"; 

echo '7:: '; 
foreach(get_option('tt_options') as $key => $value) { 
    echo $key.' : '; 
    echo $value."<br>"; 
} 

我从以前的代码中得到的结果如下所示:

0:: Array ([last_tab] => 1 [footer_text] => Default Text2) 
1:: Default Text2 
2:: Default Text2 
3:: 
array (size=2) 
    'last_tab' => string '1' (length=1) 
    'footer_text' => string 'Default Text2' (length=13) 
4:: 
string 'Default Text2' (length=13) 
5:: Array 
6:: Array ([last_tab] => 1 [footer_text] => Default Text2) 
7:: last_tab : 1 
footer_text : Default Text2 

修订1:

是@Kenney我运行不同版本的PHP。谢谢你指出。我不知道你刚刚提到的旧版PHP中的语法。 的版本是:

当地环境:5.5.12

临时环境:29年3月5日

所以,在我的情况,如果我无法控制它安装的PHP版本,只访问该值的方法是使用foreach语句?

关于整理和字符集,我在数据库

SHOW CREATE TABLE `wp_options` 

运行下面的查询,并得到了在这两种环境:

DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci 

我也检查了我的wp-config.php文件的文件和什么我发现是:

分期环境:

define('DB_CHARSET', 'utf8mb4'); 
define('DB_COLLATE', ''); 

当地环境:

define('DB_CHARSET', 'utf8'); 
define('DB_COLLATE', ''); 

修订2:

非常感谢@Kenney!这很有帮助!确实存在语法错误:

Parse error: syntax error, unexpected '[', expecting ',' or ';' in footer.php on line 66 

这是您之前陈述的原因。

综上所述帮助,可能需要在这个线程结束了另外一个人,用

echo get_option('tt_options')['footer_text']; 

与旧的PHP版本5.4.0可能无法正常工作,但

$temp = get_option('tt_options'); 
echo $temp['footer_text']; 

一样。

As of PHP 5.4 you can also use the short array syntax, which replaces array() with [].

参考: http://php.net/manual/en/language.types.array.php

+0

检查你的数据库表中的排序规则和你的PHP数据库驱动程序在这两种环境的编码设置。 – Calimero

+7

您正在运行不同版本的PHP;不是所有的人都支持语法'somefunc()['somefield']';使用一个临时变量。 – Kenney

+1

*“存储在数据库中的序列化数据”* ...现在我有一个伤心:( – CD001

回答

0

正如@ miken32提到的,让我来回答这个问题,而不是编辑它的,为了防止这个问题,从显示未答复。

问题是我的临时环境中的PHP版本。它低于版本5.4.0,因此它不支持短数组语法。

综上所述帮助,可能需要在这个线程结束了另外一个人,用

echo get_option('tt_options')['footer_text']; 

与旧的PHP版本5.4.0可能无法正常工作,但

$temp = get_option('tt_options'); 
echo $temp['footer_text']; 

一样。诀窍是在访问所需值之前使用临时变量。

As of PHP 5.4 you can also use the short array syntax, which replaces array() with [].

参考: http://php.net/manual/en/language.types.array.php