0

I've got a database with utf8_general_ci encoding, but when I fetch the data with php to the page it shows "????", what's wrong?

<?php
$query1 = "SELECT name FROM `kotegorii`";
$result1 = mysql_query($query1) or die(mysql_error());
echo "<h3>Категориялар</h3>";
while ($row = mysql_fetch_array($result1)) {

    echo $row['name']."<br>";
}
?>

the code is written using the UTF-8 encoding also..

Hurrem
  • 193
  • 2
  • 4
  • 15
  • Is the field that the data is stored in utf8_general_ci also as you can have a database that is utf8 and the database columns can be different!? – llanato Mar 27 '13 at 09:27
  • 1
    Recommended reading: [UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – Álvaro González Mar 27 '13 at 09:28
  • yes, all the tables and all the rows are utf8_general_ci – Hurrem Mar 27 '13 at 09:30
  • Kind of off topic, but PHP's _mysql_ extension is now obsolete. One should not use the mysql_* functions. http://uk3.php.net/manual/en/intro.mysql.php – marekful Mar 27 '13 at 09:45
  • You might use the wrong encoding to either display the website in your browser or when you send data into your database. From your question alone, this can not be exactly said. As it's not answered until now, I vote tot delete it as it's not really a good question either. – hakre Jul 25 '13 at 12:48

3 Answers3

7

You have to set the MySQL connection encoding to UTF-8 right after connecting to the database with mysql_set_charset:

mysql_set_charset('utf8');

The server uses the connection encoding to send information to the client, no matter how the text is encoded in the storage. The connection encoding is latin1 by default on many installations.

Also, you shouldn't use the old mysql API in new code; it is deprecated and will be removed from PHP in some future version.

Joni
  • 108,737
  • 14
  • 143
  • 193
  • This extension is deprecated as of PHP 5.5.0. So you have to use mysql_query("set names utf8;"); – Adem Öztaş Mar 27 '13 at 09:38
  • @AdemÖztaş The entire mysql extension is deprecated. Even so, `mysql_set_charset` is preferred to `SET NAMES` because it will also work for encodings that are not compatible with ASCII, and because it is required for `mysql_real_escape_string` to work. – Joni Mar 27 '13 at 09:47
  • Correction: neither can be used to set an encoding that is not compatible with ASCII. – Joni Mar 27 '13 at 10:19
  • Man, you are boss. It is a pitty that I met your answer after 3 hours of lurking internet – scarface Sep 11 '17 at 21:01
0

You should set page encoding too.

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
    <!-- and so on -->
Kasyx
  • 3,170
  • 21
  • 32
0

I don't know how your data looks but have you tried utf8_decode utf8_encode on $row['name']?

Kasyx
  • 3,170
  • 21
  • 32
Ogelami
  • 366
  • 1
  • 14