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.

11
  • print_r( json_encode($rows)); Commented Apr 6, 2016 at 11:25
  • print_r( json_encode(($rows)); also check what is the output of var_dump($rows);. check and tell Commented Apr 6, 2016 at 11:29
  • Please run var_dump(json_encode($rows), json_last_error()) and update the question with the information. Commented Apr 6, 2016 at 11:33
  • print_r(json_encode(($rows)) doesn't work. var_dump($rows) get the array data shown as per expected. Commented Apr 6, 2016 at 11:38
  • 2
    You have an encoding error: Malformed UTF-8 characters, possibly incorrectly encoded Commented Apr 6, 2016 at 11:48

2 Answers 2

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.

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

1 Comment

Thank much this help my debugging. Not sure who gave a down vote. I revote it back .
-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.

2 Comments

var_dump($rows) confim having value. Using fetch_assoc() and print_r(json_encode($rows,1)) produces no result too :(
@Elye why don't you put the output of var_dump($rows); in your question? more good if you use echo "<pre/>";print_r($rows); and put that result in your question by editing it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.