Can’t access serialized data

I must be missing something very simple here but I can’t figure it out. I need to access serialized data which is stored in a database.
In the database the stored value is

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

I need to get the ‘Default Text2’.

Read More

In my local environment i echoed

get_option( 'tt_options' )['footer_text'] 

and got the value I needed.

The problem arises in my staging environment. There, when I type

get_option( 'tt_options' )['footer_text'] 

the page crashes, although

get_option( 'tt_options' )

works fine.

The only difference I can think of between my local and my staging environment is that the tables in the database use different engines:

Staging environment:
Engine: MyISAM

Local environment:
Engine: InnoDB

By the way, I am using WordPress if that helps.

Anyway, I tried to access the value in several ways shown below. The lines that have been commented cause problem only in my staging environment. What i found interesting is that when i try to access the value from serialized data that is not stored in the database, but in a variable (see below:

echo ($returnValue['footer_text']);

), I get the value I need. Does anyone have any idea of what is causing the problem in order to point me in the right direction? Thank you in advance for your time and guidance.

$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>";
}

The results I get from the previous code are shown below:

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

Revision 1:

Yes @Kenney I am running different versions of php. Thank you for pointing it out. I didn’t know what you just mentioned about the syntax in older versions of PHP.
The versions are:

local environment: 5.5.12

staging environment: 5.3.29

So, in my situation, if I can’t control the php version which is installed, the only way to access the value is by using a foreach statement?

Regarding the collation and charset, I run the following query at the database

SHOW CREATE TABLE  `wp_options`

and got in both environments:

DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

I also checked my wp-config.php files and what I found was:

staging environment:

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

local environment:

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

Revision 2:

Thanks a lot @Kenney ! That was helpful! Indeed there was a syntax error:

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

which was produced for the reason you previously stated.

To sum up for helping another person that might end up at this thread, using

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

with an older PHP version that 5.4.0 might not work, but

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

does.

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

Reference:
http://php.net/manual/en/language.types.array.php

Related posts

1 comment

  1. As @miken32 mentioned, let me answer the question instead of editing it, in order to prevent the question from showing as unanswered.

    The problem was the PHP version in my staging environment. It was lower than version 5.4.0, so it didn’t support short array syntax.

    To sum up for helping another person that might end up at this thread, using

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

    with an older PHP version that 5.4.0 might not work, but

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

    does. The trick is the use of a temporary variable before accessing the needed value.

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

    Reference:
    http://php.net/manual/en/language.types.array.php

Comments are closed.