/    Sign up×
Community /Pin to ProfileBookmark

Unserialize stopped working?!?!?

Hi, I’m banging my head against a brick wall trying to figure this one out…

I have this array….

[code=php]
<?php
array (
‘ebay_mode’ => ‘sandbox’,
‘sitename’ => ‘The On-line Chemical &amp; Packaging Experts’,
‘timezone’ => ‘+6’,
‘header_image_max_height’ => ‘150’,
)
?>
[/code]

I have serialized it and inserted it into the database and it looks like this…

[code=php]
<?php
a:4:{s:9:”ebay_mode”;s:7:”sandbox”;s:8:”sitename”;s:59:”APC-Pure.com – The On-line Chemical & Packaging Experts”;s:8:”timezone”;s:2:”+6″;s:23:”header_image_max_height”;s:3:”150″;}
?>
[/code]

I have a working query…

[code=php]
<?php
$preferences = mysql_query(“SELECT * FROM preferences WHERE pref_name = ‘siteprefs'”) or die (mysql_error());
while ($prefs = mysql_fetch_array($preferences))
{
$pref = unserialize($prefs[‘pref_value’]);
}
?>
[/code]

The wierd thing is this was working!! I don’t think I changed anything as I have been working on something else but I suppose there may be a conflict somewhere. I just can’t find it!!

[code=php]
<?php
echo $pref[‘sitename’];
?>
[/code]

The above echo statement gives me nothing at all on screen!! Very frustrating as I’m going to feel like a right numpty if someone points out a silly error somewhere!!

Hoping someone does ?

Regards

Smithster

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 01.2011 — I'd start by verifying that something was actually retrieved from the DB, such as with mysql_num_rows(). At least then you would know whether it's a query error or something else.
Copy linkTweet thisAlerts:
@smithsterauthorMar 01.2011 — I should have mentioned I did check that first to make sure that mysql_num_rows came back with 1! I can even echo out the serialized string too.

In fact, it's wierd because this works fine....
[code=php]
<?php
$str = serialize(array (
'ebay_mode' => 'sandbox',
'sitename' => 'The On-line Chemical &amp; Packaging Experts',
'timezone' => '+6',
'header_image_max_height' => '150',
));
$pref = unserialize($str);
?>
[/code]

So it does give me the impression it's to do with maybe the way it's stored in the database or maybe the way it's retrieved?!?!? It really was working yesterday! I think.....well I'm thinking now I may have dreampt it lol but it should still work!!!

Well I got this far typing and decided to try echoing out the serialized string...

I then viewed the source and copied the string exactly from the source into the database, it's now working again ha ha!! I can only think that somehow maybe some whitespace came about in the database - no idea, one of lifes misteries, I'm just glad it's working again!!

Thanks anyway

Regards

Smithster
Copy linkTweet thisAlerts:
@eval_BadCode_Mar 01.2011 — You need strip slashes, I would be surprised if you could store serialized data without escaping all of those double quotes.

My [I]guess[/I] is that when unserialize is seeing the string with slashes in it, it just said "this isn't a serialized object at all, return null/false".

When you try to echo null/false you don't get an error, it just doesn't have any output at all.

Try this bit of debug code:
[code=php]
<?php
$preferences = mysql_query("SELECT * FROM preferences WHERE pref_name = 'siteprefs'") or die (mysql_error());
while ($prefs = mysql_fetch_array($preferences)) {
echo "<pre>[",get_type($prefs),"][",get_type($prefs['pref_value']),"]
",unserialize($prefs['pref_value']),"
-------------------------------------</pre>n";
}
?>
[/code]
Copy linkTweet thisAlerts:
@NogDogMar 01.2011 — You need strip slashes, I would be surprised if you could store serialized data without escaping all of those double quotes.

My [I]guess[/I] is that when unserialize is seeing the string with slashes in it, it just said "this isn't a serialized object at all, return null/false".

When you try to echo null/false you don't get an error, it just doesn't have any output at all.

Try this bit of debug code:
[code=php]
<?php
$preferences = mysql_query("SELECT * FROM preferences WHERE pref_name = 'siteprefs'") or die (mysql_error());
while ($prefs = mysql_fetch_array($preferences)) {
echo "<pre>[",get_type($prefs),"][",get_type($prefs['pref_value']),"]
",unserialize($prefs['pref_value']),"
-------------------------------------</pre>n";
}
?>
[/code]
[/QUOTE]


Double quotes should not matter, assuming the string was wrapped in single quotes in the SQL. Also, any back-slashes used in the SQL (such as by using mysql_real_escape_string()) should not have ended up in the actual data, just as in PHP if you do an [b]echo """;[/b] the output would just be the double quote without the slash. But all that aside, your debug code would certainly be useful to find out what was actually retrieved from the DB -- if anything -- so that we can find out what to focus on. ? (I'm placing my bet on some silly typo we haven't seen/noticed yet.)
Copy linkTweet thisAlerts:
@smithsterauthorMar 03.2011 — At the moment as the serialized array is being put into the database manually, I don't have much to go off to find the problem. I say problem because on every page I include a header.php page which sets up most of the queries which layout the page. But for some reason, even though I have the above query in header.php, I have to put the exact same query on each page to allow it to work even though they all include header.php anyway! Having said that, there is one particular page which uses the query (functions.php) and I just can't get it to work at all. As before, it will echo out the string so I know not a query issue. I think it's just failing to be recognised as a php string...

I have seen unserialized arrays being stored in the database but I have no idea how to pull them out for use. I think this will be my only option as I am having no luck with using serialization.

So....inside a mysql field I now have...
[code=php]
array('ebay_mode' => 'sandbox', 'sitename' => 'The On-line Chemical &amp; Packaging Experts', 'timezone' => '+6', 'header_image_height' => '150',)
[/code]

By the way, my reason for this is that a lot of the website is dependent on the array values which need to be changed from time to time. If I can get it to work this way, it will save me having to write out about 10 select queries for each preference.

Hope someone can help

Regards

Smithster
Copy linkTweet thisAlerts:
@NogDogMar 03.2011 — ...

I have serialized it and inserted it into the database and it looks like this...
[code=php]
<?php
a:4:{s:9:"ebay_mode";s:7:"sandbox";s:8:"sitename";s:59:"APC-Pure.com - The On-line Chemical & Packaging Experts";s:8:"timezone";s:2:"+6";s:23:"header_image_max_height";s:3:"150";}
?>
[/code]

...[/QUOTE]


Does it actually have the <?php and ?> tags in the database? (They should not be part of the serialized value.)
Copy linkTweet thisAlerts:
@smithsterauthorMar 03.2011 — no the database field contains only the part in between the tags.
Copy linkTweet thisAlerts:
@NogDogMar 03.2011 — The direct problem is this part of the serialized array:
<i>
</i>s:59:"APC-Pure.com - The On-line Chemical &amp; Packaging Experts"

The "s:59" part means that the value is a string that is 59 characters long, but the value provided is only 54 characters long. As to why, you'll have to look at how the data got created in the first place. For a quick fix, you could directly change it to "s:54" in the DB.
Copy linkTweet thisAlerts:
@smithsterauthorMar 03.2011 — thanks for pointing that out, I never knew the numbers represented the number of chars! However, since posting the above string, I have re-serialized the string, and just checked it and it is the correct string length. I still don't know why I have to recreate the query on every page even though it is on the header.php page which is included in every page!
Copy linkTweet thisAlerts:
@NogDogMar 03.2011 — thanks for pointing that out, I never knew the numbers represented the number of chars! However, since posting the above string, I have re-serialized the string, and just checked it and it is the correct string length. I still don't know why I have to recreate the query on every page even though it is on the header.php page which is included in every page![/QUOTE]

Maybe a scoping issue? (E.g.: if it's only within a function on the header script.) Or if you are including the header script via URL instead of a file system path/name?
Copy linkTweet thisAlerts:
@smithsterauthorMar 03.2011 — Nope, it's not that, no functions in the header.php file and yes it is included as....

include('header.php');

I managed to narrow it down to find that the query doesn't have to be on every page, only the unserialize part.... like this...

[code=php]
$pref = unserialize($str);
[/code]


Means I do this

header.php....
[code=php]
$result = mysql_query("SELECT * FROM preferences WHERE pref_name = 'siteprefs'") or die (mysql_error());
while ($row = mysql_fetch_array($result))
{
$str = $row['pref_value'];
}
$pref = unserialize($str);
[/code]

anyotherpage.php...
[code=php]
include('header.php');
$pref = unserialize($str);

//the rest of my script
[/code]

It's just frustrating but atleast it's now only 1 line of code!

Regards

Smithster
Copy linkTweet thisAlerts:
@NogDogMar 03.2011 — Weird. Probably some silly error you're just not seeing. (At least that's usually the case when something weird like that happens to me. ? )
Copy linkTweet thisAlerts:
@hastxMar 04.2011 — is the '&amp;' getting transposed to '&' somewhere and accounting for the missing characters?
×

Success!

Help @smithster spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...