1

After fetching from the DB, I perform a print json_encode on it as below

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

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;
}

It doesn't print anything. After debugging i.e. var_dump etc, simplify the query, reducing the data result... I can't to realize this is because one of the field (description) contain single quote (i.e. ') in it. With the existence of this single quote, I can't get my json_encode working.

Is there anyway to work around this, than to go to the database and manually edit all the data with escape character? Perhaps a way to replace the auto-escape the single-quote after it got retrieve from the db?

UPDATED Apparently this is not due to single quote issue, but a special single quote call right single quote mark. Check http://unicodelookup.com/#’'/1

Found another one , which is – and -. Refer to http://unicodelookup.com/#–-/1. The first one will cause the json_encode not to return anything.

Another one is “ and ". http://unicodelookup.com/#“"/1

Elye
  • 53,639
  • 54
  • 212
  • 474
  • 1
    Show us where you got $rows from. – Wayne Whitty Apr 06 '16 at 12:51
  • Please show us the format of the value you fetched from the database – WebInsight Apr 06 '16 at 12:52
  • An example of a field is having as below , `EPCC’s Main Auditorium`. Notice the after the EPCC, there's single quote. – Elye Apr 06 '16 at 12:53
  • http://stackoverflow.com/questions/2275359/jquery-single-quote-in-json-response see this answer – WebInsight Apr 06 '16 at 12:55
  • Validate your json output with http://jsonlint.com/ – Chay22 Apr 06 '16 at 13:00
  • Found out that apparently the issue is not 'single quote' i.e. apostrophe, but right single quotation mark. They are ’ that is not working, while ' that is working. Refer to http://unicodelookup.com/#’'/1 to get the info. – Elye Apr 06 '16 at 13:38

1 Answers1

1

Single Quote shouldn't affect your json_encode() function.

Example.

$person = array(
    "name" => "I don't know my Name",
    "email" => "I don't know my Email as well"
);   
print_r($person);

Output

Array
(
    [name] => I don't know my Name
    [email] => I don't know my Email as well
)

and

echo json_encode($person);

Output

{"name":"I don't know my Name","email":"I don't know my Email as well"}
Shadab Mehdi
  • 604
  • 10
  • 23
  • Thanks, this enlighten me to check further. Apparently the character in the db is not the usual single quote, but right single quotation mark. They are ’ that is not working, while ' that is working. Refer to http://unicodelookup.com/#’'/1 to get the info. – Elye Apr 06 '16 at 13:39