0

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?

Dharman
  • 30,962
  • 25
  • 85
  • 135
ENBYSS
  • 819
  • 1
  • 10
  • 22
  • 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. – Zoe Edwards Apr 30 '18 at 09:09
  • 1
    I think you are using `echo` before you set the header. This might cause problems. – Brainfeeder Apr 30 '18 at 09:11
  • remove `header('Content-Type: application/json');` – Alive to die - Anant Apr 30 '18 at 09:12
  • please post ajax call also –  Apr 30 '18 at 09: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. – ENBYSS Apr 30 '18 at 10:14
  • @ThomasEdwards the `print_r` command prints the contents of the database, and `json_last_error` returns 5. Guessing that's not good... – ENBYSS Apr 30 '18 at 10:17
  • Now we are moving toward completing the question and making it on-topic. We need all error codes and echoed checkpoints of working code. – mickmackusa Apr 30 '18 at 10:22
  • If I'm reading this right, the problem seems to be with the syntax? – ENBYSS Apr 30 '18 at 10:24
  • Possible duplicate of https://stackoverflow.com/q/279170/2943403 – mickmackusa Apr 30 '18 at 10:27
  • The content of $rows must be utf8 encoded. – odan Apr 30 '18 at 10:51
  • @DanoelO we could not have given this advice earlier because the OP didn't give all of the vital info. Now it can be closed with UTF-8 All the way through or similar. – mickmackusa Apr 30 '18 at 11:46
  • Relevant: https://stackoverflow.com/q/10199017/2943403 – mickmackusa Apr 30 '18 at 11:51

4 Answers4

7

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.

ENBYSS
  • 819
  • 1
  • 10
  • 22
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.

Brainfeeder
  • 2,604
  • 2
  • 19
  • 37
-2

Try this:

echo "<pre>";
print_r(json_encode($rows, JSON_PRETTY_PRINT));
echo "</pre>";
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
CK Wong
  • 68
  • 6
-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
Matt Ke
  • 3,599
  • 12
  • 30
  • 49