1

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?

12
  • 2
    After your test line, can you write print_r($rows); and paste what it prints – removing any sensitive information. Also, you can try using echo json_last_error(); at the end to find out the error. Commented Apr 30, 2018 at 9:09
  • 1
    I think you are using echo before you set the header. This might cause problems. Commented Apr 30, 2018 at 9:11
  • remove header('Content-Type: application/json'); Commented Apr 30, 2018 at 9:12
  • please post ajax call also Commented Apr 30, 2018 at 9:14
  • Removing the header, or using echo after the header instead still returns nothing apparently. Also, I'm not calling this with ajax, I'm loading the entire php file to see its output. It returns JSON with the single element, but nothing with the whole array though. Commented Apr 30, 2018 at 10:14

4 Answers 4

8

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Please show the sample text that causes the issue (as an edit on the question). When you post a new question include errors before posting.
It's a huge wall of text, I can't exactly share it without it seeming incoherent.
I fixed it by changing the connection charset $mysqli->set_charset("utf8")
4

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

-2

Try this:

echo "<pre>";
print_r(json_encode($rows, JSON_PRETTY_PRINT));
echo "</pre>";

Comments

-2

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

Why not use JSON exceptions? e.g. json_encode($yourresult, JSON_THROW_ON_ERROR);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.