0

I writed a simple php file (recalled using GET method), that returns a json file. The code follows here:

$q=$_GET["q"];

$query = "SELECT name, surname, data, session from users where id= ?";
      $stmt = $connectiondb->prepare($query);
      $stmt->bind_param('i', $id);
      $id= $q;
      $stmt->execute();
      $stmt->bind_result($name,$surname,$data,$session);
      $stmt->fetch();
      $stmt->close();

      $array = array("name" => $name, "surname" => $surname,"data" => $data, "session" => $session); 
      echo json_encode($array);

The field "session" in the table of my database (mySql) is definied in this way:

Type : text
Collation: utf8_general_ci

I tried to launch the script selecting a row from the table "users" where session has the value "last session" and the result is the following:

{"name":"John","surname":"Mayer","date":"2013-01-01","session":"last session"}

and this is good. But when the filed session in my table has the value "1° session" the script generates the following output:

{"name":"Mike","surname":"Stern","date":"2013-04-02","session":null}

(as you can sea session is null in json output)

I think that the problem is generated by the character °. How can I solve this problem?

EDIT:

I tried adding the following line of code php:

$session = utf8_decode($session);

and I obtain this:

{"name":"Mike","surname":"Stern","date":"2013-04-02","session":"1? session"}

I have no longer the null value but the ° is translated into ? character.

GVillani82
  • 17,196
  • 30
  • 105
  • 172
  • Could you show the content of your table as returned by `SELECT * FROM users` ? – Sylvain Leroux Jul 09 '13 at 18:06
  • @SylvainLeroux If I print the variable php session it returns me "1° session" – GVillani82 Jul 09 '13 at 18:12
  • @SylvainLeroux I'm sure this is the correct way. The variables php are all correctly created. look here http://php.net/manual/it/mysqli-stmt.bind-param.php for the uso of bind_param – GVillani82 Jul 09 '13 at 18:14
  • 1
    `json_encode` requires data to be UTF-8. Your **database connection** likely isn't set to UTF-8. See [UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – deceze Jul 09 '13 at 18:29
  • The answer in the suggested post does not solve my problem, because the character ° is translated into \u00b0 sequence and if I try to convert it to charcater using utf8_decode() I obtain null – GVillani82 Jul 09 '13 at 19:12
  • If "°" is encoded to `\u00b0`, **that's correct!** That's perfectly fine JSON encoding for "°". You have fixed the problem! You don't need to `utf8_decode` anything. – deceze Jul 09 '13 at 19:26
  • Read about what `utf8_decode` does → [What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text](http://kunststube.net/encoding/) – deceze Jul 09 '13 at 19:26

1 Answers1

1

As suggested in this post

I add this line:

$connectiondb->set_charset('utf8');

the result is the following:

{"name":"Mike","surname":"Stern","date":"2013-04-02","session":"1\u00b0 session"}

This seems a problem cause I want ° and not the relative UTF8 code.

But, since the php file is called form ajax I simply setted

scriptCharset: "utf-8"

and now I can be refer to data.session and obtain "1° session"

Community
  • 1
  • 1
GVillani82
  • 17,196
  • 30
  • 105
  • 172
  • 1
    `scriptCharset` shouldn't do anything, since the character is not UTF-8 encoded! It's *JSON escaped*. If you simply parse the JSON with a proper JSON parser, it'll be parsed and the original character restored. – deceze Jul 09 '13 at 19:42