0

I have two functions below that query the PHP and then print the json out. I tried on my local machine AMPP Apache server, everything works with the Json printed. When I tried on my host machine (with all DB setup, tested good), somehow the print json_encode($rows) doesn't print anything on the remote host server.

So I added the debug echo sizeof($rows) below, and indeed there are 8 records as per expected. What can't the print json_encode($rows) print anything? How to further debug?

function queryPrintJson($cnx, $query) {
    $rows=queryReturnJsonArray($cnx, $query);       
    echo sizeof($rows);
    print json_encode($rows);
}

function queryReturnJsonArray($cnx, $query) {
    $result=mysqli_query($cnx, $query) or die ("Can't execute query!");
    $rows = array();

    while($obj = $result->fetch_object()){
        $rows[] = $obj;
    }
    $result->close();
    return $rows;
}

p/s: the same function works on the same remote host-server, for another db. It also works on my localhost.

*UPDATED 'var_dump(json_encode($rows), json_last_error())' shows bool(false) int(5). Don't know what it means.

My query is simply $newsquery = "SELECT * FROM newstbl where Status = 1";

*UPDATE

After further debugging, I found that one of the field which is a description field that has long data... That field if omitted, everything works. But if included, it doesn't print out.

This also could imply not related to UTF-8 as well. As the description is all in normal english character. Hence doesn't seems like a duplicate of the other question.

p/s: Not sure who gave the down-vote to all the answers below, as it does help my debugging. Whoever does that need to be responsible before giving a down vote to the below answer. They all does help my debugging.

Elye
  • 53,639
  • 54
  • 212
  • 474

2 Answers2

0

Add some debug tracing to find out what's going on:

function queryPrintJson($cnx, $query) {
    $rows=queryReturnJsonArray($cnx, $query);       
    echo sizeof($rows);

    // only for debug
    $tmp = json_encode($rows);
    var_dump($rows, $tmp);

    print json_encode($rows);
}

...and check what's wrong with the contents of your variables.

MarcM
  • 2,173
  • 22
  • 32
  • Thank much this help my debugging. Not sure who gave a down vote. I revote it back . – Elye Apr 06 '16 at 12:21
-1

var_dump($rows) to confirm there are values stored in it. I guess if it's empty it's because you use fetch_object() instead of fetch_assoc()

And if that's correct use : print_r(json_encode($rows,1)) to print out an associative array.

  • `var_dump($rows)` confim having value. Using `fetch_assoc()` and `print_r(json_encode($rows,1))` produces no result too :( – Elye Apr 06 '16 at 11:52
  • @Elye why don't you put the output of `var_dump($rows);` in your question? more good if you use `echo "
    ";print_r($rows);` and put that result in your question by editing it.
    – Alive to die - Anant Apr 06 '16 at 11:55