Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialized array breaks on retrieval from database

I am saving data in a mysql database. This data is an array and the content is different data of the current user that is logged in to my system.

I do this when I save to the database:

$data = addslashes(serialize($array));

then

"UPDATE or INSERT INTO TABLE SET ... data = '$data';"

Now, the data are saved correctly since the insert or update statement return valid from my php code.

My problem is when I try to un-serialize it, it returns false and a notice is shown in my page.

What am I doing wrong?

like image 491
Gilbert Kakaz Avatar asked Dec 23 '11 03:12

Gilbert Kakaz


1 Answers

I will bet the field in your mysql database is not big enough to save all the characters. That is why, when you un-serialize it you get an notice and nothing in return.

Try to increase the field to a MEDIUMBLOB or MEDIUMTEXT (maximum length of 16,777,215) or LONGBLOB or LONGTEXT (maximum length of 4,294,967,295) like this:

ALTER TABLE your_table MODIFY COLUMN column_name MEDIUMTEXT /* other properties*/;

And try to save and read your data again.

Now, if your data is over 4,294,967,295 (which is LONGBLOB or LONGTEXT), maybe you should check what kind of data you saving and maybe filter or remove unwanted some.

like image 71
Book Of Zeus Avatar answered Oct 22 '22 05:10

Book Of Zeus