11 Replies - 16436 Views - Last Post: 30 September 2013 - 03:08 AM Rate Topic: -----

#1 Keylogger   User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 348
  • Joined: 14-February 11

json_encode special characters

Posted 27 September 2013 - 09:32 AM

Hello guys,

I've been searching and trying different types of solutions across the web, but I can't figure out how to solve this!
I select from database data and then I need to display with json_encode.

$host = "localhost";
$user = "root";
$pass = "";
$database = "myDB";

$db = new mysqli($host, $user, $pass, $database);
$db->set_charset('utf8');
if ($db->connect_errno) {
	printf("Error " . $db->connect_error);
	exit();
}
$query = $db->prepare("SELECT name FROM `table` where id = 770");
$query->execute();
$query->bind_result($name_);
$query->store_result();
$all = array();
while($query->fetch()){
	$all[] = array('name' => $name_);
}
echo json_encode($all);

This should return:
[{"name":"Acessůrios"}]

But it's returning:
[{"name":"Acess\u00f3rios"}]

And this are the things that I've tried so far so I could display the words properly:
$db->set_charset('utf8');
json_encode($all, JSON_UNESCAPED_UNICODE)
iconv("UTF-8", "UTF-8//IGNORE", $name_);
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
header('Content-Type: text/html; charset=utf-8');
utf8_encode($name_)
htmlentities( (string) $name_, ENT_QUOTES, 'utf-8', FALSE);
$query_A = $db->prepare("SET NAMES UTF8");


Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: json_encode special characters

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,573
  • Joined: 08-June 10

Re: json_encode special characters

Posted 27 September 2013 - 09:35 AM

View PostKeylogger, on 27 September 2013 - 06:32 PM, said:

But it's returning:
[{"name":"Acess\u00f3rios"}]

donít worry, JS can read that just fine.

PS. thereís no need to use a Prepared Statement for a constant query.
Was This Post Helpful? 0
  • +
  • -

#3 Keylogger   User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 348
  • Joined: 14-February 11

Re: json_encode special characters

Posted 27 September 2013 - 10:08 AM

Actually, it's not very fine :P
I use this as Webservice for a Android Application. And I cannot show in the android: Acess\u00f3rios

Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,573
  • Joined: 08-June 10

Re: json_encode special characters

Posted 27 September 2013 - 10:13 AM

other question, how is that string saved in the DB? if itís already like that in the DB it might be more problematic than otherwise.
Was This Post Helpful? 0
  • +
  • -

#5 Keylogger   User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 348
  • Joined: 14-February 11

Re: json_encode special characters

Posted 30 September 2013 - 01:13 AM

Hello,

No. In the DB it's with the right encoding. What can I do?
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,573
  • Joined: 08-June 10

Re: json_encode special characters

Posted 30 September 2013 - 01:24 AM

try it without JSON_UNESCAPED_UNICODE (just for comparison).

Quote

$db->prepare("SET NAMES UTF8");

reading the manual, utf8 should be quoted.

btw. which version of PHP are you using?

in case youíre not on PHP 5.4+, cf. http://www.php.net/m...code.php#105789

This post has been edited by Dormilich: 30 September 2013 - 01:29 AM

Was This Post Helpful? 0
  • +
  • -

#7 Keylogger   User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 348
  • Joined: 14-February 11

Re: json_encode special characters

Posted 30 September 2013 - 02:14 AM

I'm using 'PHP Version 5.5.1' -> displayed by the function phpinfo().

I already have tried that option:
$query = $db->prepare("SET NAMES UTF8");
$query->execute();

I'm working on notepad++ and the coding is: "UTF-8 WITHOUT BOOM", I think it's right.
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,573
  • Joined: 08-June 10

Re: json_encode special characters

Posted 30 September 2013 - 02:42 AM

View PostKeylogger, on 30 September 2013 - 11:14 AM, said:

I already have tried that option:

but you didnít quote the value.

Quote

I'm working on notepad++ and the coding is: "UTF-8 WITHOUT BOOM", I think it's right.

1) itís BOM, not BOOM
2) itís good that your script is encoded in UTF-8
3) that doesnít concern the data you send into (or retrieve from) the DB.
Was This Post Helpful? 0
  • +
  • -

#9 Keylogger   User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 348
  • Joined: 14-February 11

Re: json_encode special characters

Posted 30 September 2013 - 02:48 AM

I didn't quoted the value? I don't get it. It's quoted.
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,573
  • Joined: 08-June 10

Re: json_encode special characters

Posted 30 September 2013 - 02:49 AM

the SQL statement is quoted, the value UTF8 is not.
Was This Post Helpful? 0
  • +
  • -

#11 Keylogger   User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 348
  • Joined: 14-February 11

Re: json_encode special characters

Posted 30 September 2013 - 03:06 AM

$query_A = $db->prepare("SET NAMES 'UTF8'");
$query_A->execute();

Well, it's the same. It comes with special characters. I presume "NAMES" it's a global method/function..
Was This Post Helpful? 0
  • +
  • -

#12 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,573
  • Joined: 08-June 10

Re: json_encode special characters

Posted 30 September 2013 - 03:08 AM

if in doubt, read the Manual.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1