George32x
George32x

Reputation: 47

PHP reading a mysql data encoded in utf8_unicode_ci

I have the following php code:

<?php
mysql_connect("localhost","root","*****");
mysql_select_db("MyData");
$sql=mysql_query("select * from menu");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>

My data base is encoded on utf8_unicode_ci. So when I read the output in a browser the latin characters a presented well but greek characters are presented with "???????".When I read the data from my data base are presented regularly. Can someone help me about what do I have to do with my php code?

Upvotes: 1

Views: 8890

Answers (5)

Efmus
Efmus

Reputation: 11

PHP :

header('Content-Type: text/html; charset=utf-8');

mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES utf8");

HTML :

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

File : Convert file to UTF-8 using notepad

Upvotes: 1

PayToPwn
PayToPwn

Reputation: 1257

I solved it in a similar way as MBarsi, adding a line of code after selecting the database, but using mysqli:

$mysqli->select_db("database"); 
$mysqli->set_charset("utf8");

Upvotes: 0

millebii
millebii

Reputation: 1287

Have a look at your html encoding, because it is likely to be ISO-LATIN-1 default and thus your display is the issue not the sql. In that case you want to do the following
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head> <?php print(json_encode($output)); ?>
</html>

Upvotes: 0

MBarsi
MBarsi

Reputation: 2457

add these 2 queries to after mysql_select_db("MyData"); :

mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES utf8");

Upvotes: 8

Marc B
Marc B

Reputation: 360872

Try

header('Content-type: text/plain;charset=UTF-8');

You'd need to do this before the print() call in your script.

Upvotes: 2

Related Questions