I have a listing of names, that I want to store in mysql as a serialized array (for caching purposes). Some names contain apostrophes, and for some reason won't un-serliaze properly. There is no output.
views:
52answers:
1
+2
A:
Use mysql_real_escape_string
on the serialized string prior to inserting into the database.
You could handle storing serialized data bybase64_encode
ing it prior to inserting it in the database, and base64_decode
ing it upon retrieval, but with proper escaping that is unnecessary.
karim79
2010-05-29 01:47:20
oh. Are you really base64'ing data to store it in the database?
Col. Shrapnel
2010-05-29 02:06:16
@Col. Shrapnel - No. I've not done that (in PHP). But theoretically, it does allow you to sidestep many caveats involved in storage of serialized strings and just 'relax'. Also, I did say 'my favourite' and not 'the be it all, end all way would be to...'. But thank's for downvoting.
karim79
2010-05-29 02:13:35
Yeah I understand. Working code at any cost. Leads you to accepted answer and more precious rep, no matter you didn't solve anything but just made a dirty patch.
Col. Shrapnel
2010-05-29 02:20:41
+1 for mysql_real_escape_string. base_64_encode is unnecessary if you're properly escaping.
Frank Farmer
2010-05-29 02:24:06
@Frank if you read the question, there is not insert problem but unserializing one.
Col. Shrapnel
2010-05-29 02:29:47
Thank you for your attention to my humble person. But I prefer to discuss programming questions.
Col. Shrapnel
2010-05-29 03:17:16
If you're inserting and selecting correctly, unserialization should work without issue. If unserializing is failing, then you're probably not storing/retrieving data correctly.
Frank Farmer
2010-05-31 16:35:50