Gilbert Kakaz
Gilbert Kakaz

Reputation: 1567

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?

Upvotes: 12

Views: 485

Answers (2)

Book Of Zeus
Book Of Zeus

Reputation: 49877

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.

Upvotes: 23

Indranil
Indranil

Reputation: 2471

After you're getting the data from the table, are you removing the slashes before unserialize function.

Try inserting without the addslashes() and add slashes before it's taken to the array.

Upvotes: 0

Related Questions