开发者

PHP json_encode producing invalid JSON when stored in MySQL

I've googled this a bit already with no joy. I am running a standard PHP array through $encoded = json_encode($myArray);

Before storing it in a MySQL database. 99% of the time this is fine but on the odd occasion the encoded JSON looks corrupted....

{ "value": " }

being stored in the database. This is a rare occasion, in fact I can't replicate it yet in my local development environment but wouldn't you know it user can!!

The data being stored comes from a <textarea> on a form so the user could be putting in dodgy characters that are breaking thing somewhere, thing is I've tried every character I can think of and have even force non UTF-8 characters into it and it never causes the above.

I'm really struggling getting to the bottom of this. First of I can't see how json_encode is producing the result above, perhaps figuring that out would help figure out wha开发者_JAVA技巧t going on but I am stuck.

Any advice, help, pointer would be awesome,

Off I go to try to replicate it.


Make sure that the field in your database is long enough. I had an issue where the json encoded data was longer than the field length and was getting truncated and causing it to be invalid... Sometimes just looking at the encoded data it is hard to see that it is cut off on the end. echo it out on your php page and compare it to the data that ends up in the field in the database...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜