Although we did not discuss how to fix all of the errors independently, I strongly believe you can take it from there as you have known what the error message. In this article, you have learned how to debug common json_encode() error and how to get them fixed. To fix it, I introduced `JSON_UNESCAPED_UNICODE`: echo( json_encode(, JSON_UNESCAPED_UNICODE) ) Conclusion This error happens if the result contains mixed encoding characters. Now, I see the error!: Malformed UTF-8 characters, possibly incorrectly encoded So I placed the code after the json_encode(…) function: echo json_last_error_msg() // Print out the error if any die() // halt the script It was not memory related.Īfter much trial and error, I decided to see what json_encode is complaining about if any. I went ahead to increase my memory limit but that did not help. But if I LIMIT the result to like 100 records, I get the JSON printed out but any record I fetch above 100 shows empty.Īt first, I thought it might be a memory issue (but 164 records is not that much □). The result was empty, nothing was printed out. In the last part of the code, I encode the array of courses to JSON then print out the result. In the code above, I’m fetching all the courses that are in the `courses` table that is about 164 records. Below is my complete code: $courses = $db = new mysqli(HOST, USERNAME, PASSWORD, DATABASE) if ($db->connect_error) echo( json_encode() ) die() I was working with a result which I fetched from the database that I converted to JSON. I encountered this error personally which prompted me to write this article. This is one of the common error you will face especially if you are fetching your data from a database. Fixing “Malformed UTF-8 characters, possibly incorrectly encoded” error Here is the list of errors you might encounter: JSONERRORINFORNAN ( int ) The value passed to jsonencode () includes either NAN or INF. If the JSONPARTIALOUTPUTONERROR option was given, null will be encoded in the place of the recursive reference. If what was printed out is - “No errors”, then there is nothing wrong with the data. The object or array passed to jsonencode () include recursive references and cannot be encoded. echo json_last_error_msg() // Print out the error if any die() // halt the script Add the below code in your code after the json_encode() function that you have. The first step in solving a problem is to first know what the problem really is.įirst, let’s see if there is any error from json_encode. In this article, I will discuss how you can fix a `json_encode` function that returns an empty result or throws an error. json_encode is a PHP function that converts an array to JSON format. If you are building a RESTful API in PHP, then you are most likely using the json_encode function. How I fixed PHP json_encode() returning empty result
0 Comments
Leave a Reply. |