Jump to content

Weird unserialize problem


doddsey_65
 Share

Recommended Posts

I have an array from the database like so

 

Array (2)
(
|    ['0'] => Array (4)
|    (
|    |    ['f_fid'] = String(1) "1"
|    |    ['0'] = String(1) "1"
|    |    ['f_names'] = String(99) "a:4:{i:0;s:18:"register_user_name";i:1;s:5:"r_u_n";i:2;s:10:"user_alias";i:3;s:12:"nickusername";} "
|    |    ['1'] = String(99) "a:4:{i:0;s:18:"register_user_name";i:1;s:5:"r_u_n";i:2;s:10:"user_alias";i:3;s:12:"nickusername";} "
|    )
|    ['1'] => Array (4)
|    (
|    |    ['f_fid'] = String(1) "2"
|    |    ['0'] = String(1) "2"
|    |    ['f_names'] = String(104) "a:4:{i:0;s:18:"register_password";i:1;s:5:"r_pass_word";i:2;s:10:"user_pass";i:3;s:12:"reg-pass-word";} "
|    |    ['1'] = String(104) "a:4:{i:0;s:18:"register_password";i:1;s:5:"r_pass_word";i:2;s:10:"user_pass";i:3;s:12:"reg-pass-word";} "
|    )
)

 

So i use a foreach loop to turn the serialized value into an array

 

foreach($fields as $key => $val)
    {
        $fields[$key]['f_names'] = unserialize($val['f_names']);
    }

 

But for some reason the last f_names is false

 

Array (2)
(
|    ['0'] => Array (4)
|    (
|    |    ['f_fid'] = String(1) "1"
|    |    ['0'] = String(1) "1"
|    |    ['f_names'] => Array (4)
|    |    (
|    |    |    ['0'] = String(18) "register_user_name"
|    |    |    ['1'] = String(5) "r_u_n"
|    |    |    ['2'] = String(10) "user_alias"
|    |    |    ['3'] = String(12) "nickusername"
|    |    )
|    |    ['1'] = String(99) "a:4:{i:0;s:18:"register_user_name";i:1;s:5:"r_u_n";i:2;s:10:"user_alias";i:3;s:12:"nickusername";} "
|    )
|    ['1'] => Array (4)
|    (
|    |    ['f_fid'] = String(1) "2"
|    |    ['0'] = String(1) "2"
|    |    ['f_names'] = Boolean(0) FALSE
|    |    ['1'] = String(104) "a:4:{i:0;s:18:"register_password";i:1;s:5:"r_pass_word";i:2;s:10:"user_pass";i:3;s:12:"reg-pass-word";} "
|    )
)

 

Anyone have any idea why?

 

Thanks

 

Link to comment
Share on other sites

While it doesn't have anything to do with your problem, you should be using a fetch_assoc statement to retrieve your data (you are using fetch_array now, giving both associative and numerical arrays in your data.)

 

If that's your actual data, it is invalid for the 2nd set of values (the length of some of the strings doesn't match what the serialized syntax says they are.)

 

What does using var_export on the data produce (so that someone here could reproduce the problem.)

 

Edit: Specifically, the strings you show in that data would produce the following serialized output -

 

a:4:{i:0;s:17:"register_password";i:1;s:11:"r_pass_word";i:2;s:9:"user_pass";i:3;s:13:"reg-pass-word";}

Link to comment
Share on other sites

heres the var_export dump

 

array ( 0 => array ( 'f_fid' => '1', 0 => '1', 'f_names' => 'a:4:{i:0;s:18:"register_user_name";i:1;s:5:"r_u_n";i:2;s:10:"user_alias";i:3;s:12:"nickusername";} ', 1 => 'a:4:{i:0;s:18:"register_user_name";i:1;s:5:"r_u_n";i:2;s:10:"user_alias";i:3;s:12:"nickusername";} ', ), 1 => array ( 'f_fid' => '2', 0 => '2', 'f_names' => 'a:4:{i:0;s:18:"register_password";i:1;s:5:"r_pass_word";i:2;s:10:"user_pass";i:3;s:12:"reg-pass-word";} ', 1 => 'a:4:{i:0;s:18:"register_password";i:1;s:5:"r_pass_word";i:2;s:10:"user_pass";i:3;s:12:"reg-pass-word";} ', ), )

Im using PDO's fetch_results. I used to use fetch_assoc within a while loop but thought that a foreach loop would work better. PDO's fetch_assoc only brings back one row if not used within a while loop.

 

Link to comment
Share on other sites

I'm not sure what's wrong, but wanted to point this out in case it means something to anyone else, I think it's what PFMaBiSmAd was refering to.

 

This is from the first index:

 

['f_names'] = String(99) "a:4:{i:0;s:18:"register_user_name";i:1;s:5:"r_u_n";i:2;s:10:"user_alias";i:3;s:12:"nickusername";} "

['1'] = String(99) "a:4:{i:0;s:18:"register_user_name";i:1;s:5:"r_u_n";i:2;s:10:"user_alias";i:3;s:12:"nickusername";} "

 

This is from the second index:

 

['f_names'] = String(104) "a:4:{i:0;s:18:"register_password";i:1;s:5:"r_pass_word";i:2;s:10:"user_pass";i:3;s:12:"reg-pass-word";} "

['1'] = String(104) "a:4:{i:0;s:18:"register_password";i:1;s:5:"r_pass_word";i:2;s:10:"user_pass";i:3;s:12:"reg-pass-word";} "

 

Notice that in the first index, f_names has "register_user_name" "r_u_n" "user_alias" etc.

 

In the second index, f_names is an EXACT duplicate of '1' from below, it doesn't have "register_user_name" "r_u_n" or "user_alias".

Link to comment
Share on other sites

even when using fetch_assoc it still brings up the same case.

 

while($fields = $sth->fetch(PDO::FETCH_ASSOC))
    {    
        $names[] = unserialize($fields['f_names']);
    }

 

a dump of $fields in the loop shows

Array (2)
(
|    ['f_fid'] = String(1) "1"
|    ['f_names'] = String(99) "a:4:{i:0;s:18:"register_user_name";i:1;s:5:"r_u_n";i:2;s:10:"user_alias";i:3;s:12:"nickusername";} "
)
Array (2)
(
|    ['f_fid'] = String(1) "2"
|    ['f_names'] = String(104) "a:4:{i:0;s:18:"register_password";i:1;s:5:"r_pass_word";i:2;s:10:"user_pass";i:3;s:12:"reg-pass-word";} "
)

 

but when i dump $names all i get is

 

Array (2)
(
|    ['0'] => Array (4)
|    (
|    |    ['0'] = String(18) "register_user_name"
|    |    ['1'] = String(5) "r_u_n"
|    |    ['2'] = String(10) "user_alias"
|    |    ['3'] = String(12) "nickusername"
|    )
|    ['1'] = Boolean(0) FALSE
)

 

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.