I am using MySQL to store a list of items on a database, and I am trying to retrieve all of the items from a single table. The connection part is working fine, and retrieving items seems to work well, but I cannot json_encode a list of items.
// Executes SQL query on the database specified by $con
$sql = "SELECT * FROM productlist";
$query = mysqli_query($con, $sql) or die(nl2br("\n Failed to execute query"));
// Retrieves all the rows returned by the SQL query
$rows = array();
while($r = mysqli_fetch_assoc($query)) {
$rows[] = $r;
}
echo json_encode($rows[0]); // test whether retrieval works
// Displays rows in content-type JSON and in PRETTY_PRINT
header('Content-Type: application/json');
echo json_encode($rows, JSON_PRETTY_PRINT);
Here is the code. The json_encode of rows[0] works well. However, when I comment it out and try to do the same with rows, it just returns nothing.
It's not even returning an error, it executes well, it's just that the json_encode function returns nothing at all when I try it with rows.
What am I doing wrong?
4 Answers 4
Answering this question in case a poor soul with a similar problem finds this.
First of all, print_r and json_last_error proved to be insanely helpful in knowing what the problem was.
First of all, check the value that json_last_error() returns, and compare it with the list of errors here. If the number returned is 5, try and check the values of the array by using the print_r function. If you see some weird characters, the probability is that the database has some wrong encoding.
In my case, changing certain columns from latin to utf-8 fixed the problem.
3 Comments
$mysqli->set_charset("utf8")You can't output something before setting a header. Unless you use output buffer (ob_ functions):
ob_start();
echo json_encode($rows[0]);
header('Content-Type: application/json');
echo json_encode($rows, JSON_PRETTY_PRINT);
ob_end_flush();
Or you could change the order in your script:
header('Content-Type: application/json');
echo json_encode($rows[0]);
echo json_encode($rows, JSON_PRETTY_PRINT);
The last option might result in a JSON parse error since you output two 'objects' here.
The json_last_error error you mention is about UTF-8 encoding. You can try to utf8 encode and decode your json.
json_encode($rows, JSON_UNESCAPED_UNICODE);
json_decode($json, false, 512, JSON_UNESCAPED_UNICODE);
If this does not solve the issue, you might want to check if everything else is UTF-8 encoded.
Comments
Try this:
echo "<pre>";
print_r(json_encode($rows, JSON_PRETTY_PRINT));
echo "</pre>";
Comments
First of all I want to thank because I solved my "big" problem with this topic. I had a result of blank page, very hard to understand why.
How to detect:
echo json_encode($yourresult) ;
json_last_error($yourresult) ;
// if the error is 5, it is UTF-8 issue
How to temporary correct with PHP 7.2:
echo json_encode($yourresult,JSON_INVALID_UTF8_IGNORE);
//wrong chars will be deleted
1 Comment
json_encode($yourresult, JSON_THROW_ON_ERROR);
print_r($rows);and paste what it prints – removing any sensitive information. Also, you can try usingecho json_last_error();at the end to find out the error.echobefore you set the header. This might cause problems.header('Content-Type: application/json');