unserializing data array with php from mysql database - php

I have the following serialized PHP array stored in a mySQL database:
a:2:{i:2070;s:4:"0.00";i:1901;s:4:"1.00";}
Now, I managed to output that value with:
$my_data=mysql_result($result,$i,"my_data");
echo "$my_data";
but I can't manage to unserialize it. I tried this but it doesn't work:
$my_data=unserialize($my_data);
When I add that in between, all I get is a blank page. Any ideas?

Maybe you should look at the process of inserting the the value into the database. Is it possible that after the values are serialized, that they were encoded in someway, such as to html entities or something?
I ran a test locally and I got the same error message. Here is output:
a:2:{i:2070;s:4:"0.00";i:1901;s:4:"1.00";} Notice: unserialize(): Error at offset 12 of 62 bytes in /srv/localhost/public_html/test.php on line 6
Here is code
<?php
$value = htmlentities('a:2:{i:2070;s:4:"0.00";i:1901;s:4:"1.00";}');
echo $value;
unserialize($value);

Related

Php loop Ajax array in mysql

I need to do a report sing php & pdf based on another coder's(asp) data in mysql.
$Mysqldata_Field1:[{"name":"alex","age":"30","pic":"filename.ext"}, {"name":"alice","age":"29","pic":"filename.ext"}]
I need to extract all the "pic" value from the array above which is stored in mysql.
What is the best approach?
I have tried this
$img=json_decode($Mysqldata_Field1,true);
foreach($img as $item=>$pic)
{
$images .= $pic["pic"].",";
}
But I'm getting error:
Invalid argument supplied for foreach()
Thanks to both the suggestion.
var_dump showed that the $mysqldata_Field1 contains both array for some records and some strings (incomplete array).
managed to correct those data entry to make a complete array, then the script worked.
this time its not the code issue, its the data entry.

PHPMyAdmin inline edit corrupting serialized data in text field

I have a serialized object stored in a mysql database in a column of type text saved via php.
If I double click the field in PHPMyAdmin to edit the value of the field inline, and then save the edit by clicking away from the edit box, the serialized data in the field is rendered corrupt somehow.
I get the following PHP error after doing this:
Notice: unserialize(): Error at offset 913 of 1951 bytes in /path/to/file.php on line 46
I am not even changing any of the data, as I am only clicking in to copy the data to the clipboard so it's not as though I'm introducing anything weird, or making an error with the syntax etc. I thought maybe some whitespace is getting added or some wierd character(s).
Is there a solution to this?
According to this question, in order to avoid errors you can try to base64 the output or serialize() before inserting it into the database:
$toDatabse = base64_encode(serialize($data)); // Save to database
$fromDatabase = unserialize(base64_decode($data)); //Getting Save Format
There is a bug in phpmyadmin that saves the '\n' of a serialized php string as line breaks. PHP serialized strings must not contain line breaks! If you want to safely modify it directly from phpmyadmin, make sure to replace the line breaks with '\n' string, this can easily be done from any text editor.

PHP unserialize() Error at Offset [duplicate]

This question already has answers here:
How to repair a serialized string which has been corrupted by an incorrect byte count length?
(17 answers)
Closed 9 years ago.
The exact error I am getting is:
ErrorException [ Notice ]: unserialize(): Error at offset 5 of 59 bytes
The serialized data returned from a TEXT field in MySQL is (Encoding: utf8, Engine: InnoDB):
a:1:{s:12:"display_name";s:6:"Foo";}
After unserializing the output is:
array(1) { [0]=> bool(false) }
I am expecting something like this:
array(1) { ["display_name"]=> string(6) "Foo" }
I am using FuelPHP 1.6.1, PHP 5.4.10, MySQL 5.5.29, InnoDB 1.1.8 on Mac OS 10.8.4. Some of the serialized entries work on unserialize while others don't, if I copy one that works and paste into one that does not it shows the same error. I believe its a character issue and I have tried to encode into utf8, urlencode, and stripslashes but nothing seems to work.
Any help is appreciated!
Update
The serialized string had a type, I did this when pasting it here for privacy to the actual display name. Here is the actual string:
a:1:{s:12:"display_name";s:3:"Foo";}
Thanks to #robw for pointing this out.
Update & Answer
I am running the serialized data through html_entity_decode() now as such:
$unserialized = unserialize(html_entity_decode($serialized, ENT_QUOTES));
Thank you for the help and advice!
Your problem is here: s:6:"Foo"
Foo is not 6 characters long... so that will never parse as expected.
Try: s:3:"Foo"
PS: This looks like you edited data in MySQL, then when your application tried to parse it, it errored out. You should never edit a serialized value in the database unless you're 100% sure you've modified the LENGTH in accordance to the TYPE and VALUE.

serialization and utf-8 in PHP

Hi All
I'm trying to serialize array that contains some utf-8 code:
....["value"]=> string(13) "مغادرة1"....
but after serializing the array,it look like this:
value";s:13:"??????
I think that the error that i get:
Message: unserialize() [function.unserialize]: Error at offset 685 of 701 bytes
is related to wrong serialization to utf-8 code
So How to serialize array that contains utf-8 code?
Thank You
Thank's for all replies
The problem was in storing the data in the database,and not in serialization,the type of field that i stored the ser content was latin,i change it to utf-8 and everything works fine

PHP unserialize error at offset, works on some servers, not others

I have code that works on a handful of servers, but not others which is coming up with serialised data. I call a page like this:
http://domain/index.php/sales/Drilldowns?params=a:12:{s:13:"selectionType";s:8:"facility";s:8:"dateType";s:5:"daily";s:10:"dateOption";s:9:"drilldown";s:6:"metric";s:13:"bookingAmount";s:9:"companyFK";s:2:"11";s:10:"facilityFK";s:0:"";s:7:"classFK";s:0:"";s:15:"customDateStart";s:4:"null";s:7:"newDate";s:10:"2010-11-01";s:10:"metricName";s:10:"Bookings%20$";s:16:"currentDateRange";s:10:"11/01/2010";s:23:"currentMetricNavigation";s:8:"DELDELTE";}&getExcel=0
This is the code I'm using:
protected function getRequestVariables(){
if(isset($_REQUEST['params'])){
var_dump($_REQUEST['params']);
echo 'length:'.strlen($_REQUEST['params']);
$vars = unserialize($_REQUEST['params']);
var_dump($vars);
} else {
$vars = $_REQUEST;
// unset saved drilldown options
$this->ci->session->svar_set('postVars', null);
}
This is a var_dump output:
string(447) "a:12:{s:13:\"selectionType\";s:8:\"facility\";s:8:\"dateType\";s:5:\"daily\";s:10:\"dateOption\";s:9:\"drilldown\";s:6:\"metric\";s:13:\"bookingAmount\";s:9:\"companyFK\";s:2:\"11\";s:10:\"facilityFK\";s:0:\"\";s:7:\"classFK\";s:0:\"\";s:15:\"customDateStart\";s:4:\"null\";s:7:\"newDate\";s:10:\"2010-11-01\";s:10:\"metricName\";s:10:\"Bookings $\";s:16:\"currentDateRange\";s:10:\"11/01/2010\";s:23:\"currentMetricNavigation\";s:8:\"DELDELTE\";}"
When that gets processed I get the following error:
A PHP Error was encountered
Severity: Notice
Message: unserialize() [function.unserialize]: Error at offset 6 of 447 bytes
Filename: plugins/Drilldowns.php
Line Number: 93
I'm trying this on 5.2.13 - works on some Linux, some OS X, not others. Have checked php.ini, charset (I think) - I can't figure it out for the life of me. I've tried things as simple as
string(18) "a:1:{s:3:\"sam\";}" length:18
and it still errors. Any clue as to why?
It's the backslashes in front of the quotes: \"
When you remove them, it works.
var_dump(unserialize('a:12:{s:13:"selectionType";s:8:"facility";s:8:"dateType";s:5:"daily";s:10:"dateOption";s:9:"drilldown";s:6:"metric";s:13:"bookingAmount";s:9:"companyFK";s:2:"11";s:10:"facilityFK";s:0:"";s:7:"classFK";s:0:"";s:15:"customDateStart";s:4:"null";s:7:"newDate";s:10:"2010-11-01";s:10:"metricName";s:10:"Bookings $";s:16:"currentDateRange";s:10:"11/01/2010";s:23:"currentMetricNavigation";s:8:"DELDELTE";}"'));
The servers this works on, probably have magic quotes turned on.
I had this problem and it took me a while to solve it. I just couldn't find any good solution but this is what I did to solve my situation:
base64_encode(serialize($User)); // make sure to encode the serialized object
unserialize(base64_decode($User)); // decode it before unserializing

Categories