0

something is going wrong , im saving json_encode response in mysql db. it sometimes saving 0 value.im 100% sure that my json_encode function does not receive 0 as a parameter because it always get empty array or associated array, so there is no any chance to receive 0 in parameter.

Problem is that why json_encode returns 0 value sometimes. i have column type longtext in db table where im saving its response. is it issue with character types? like UTF8 or non UTF8?

Here is a code snippet:

$jsonPayload = [];

if (isset($obj['answer']) && (!empty($obj['answer']) || $obj['answer'] == 0)) {
       $jsonPayload[$form_fields[$i]->id] = $obj;         
}

return json_encode($jsonPayload);
Muhammad Ateek
  • 1,057
  • 3
  • 14
  • 24
  • You are using a relational database completely wrong. You don't put an array, or anything encoded into a database. You put each value into their table column. That's what it means to use a relational database. – Charlotte Dunois Apr 24 '18 at 10:11
  • var dump the array so we can see it – delboy1978uk Apr 24 '18 at 10:19
  • `json_encode` doesn't return `0`, but it can return `false`. My bet is you have some non-UTF8 characters in your encode – Machavity Apr 24 '18 at 13:15
  • according to my investigation its return 0 due to non-UTF8 character. but dont have such character on which i can test either json_encode return 0 on passing non-UTF8 characters or not. I wants to reproduce it. – Muhammad Ateek Apr 25 '18 at 05:44

1 Answers1

0

use

json_encode($jsonPayload,JSON_UNESCAPED_SLASHES);

Probably it will work.