3

Problem: Foreign character are not displayed as they should be. This includes German, Japanese, Russian and all others excluding English (works perfectly). Ones PHP makes a call to MySQL via jQuery AJAX call it should return the information and display it on the page. The data is called and is displayed. However for non-English characters the results are displayed as a "?".

In phpMyAdmin the data displayed as it should have been be it in Japanese, German ect ect. But ones fetched from MySQL it is not returned as such.

The problem is not caused by the browser as my browser supports all Languages encodings.

MySQL encoding: UTF8_GENERAL_CI

Page encoding: UTF-8

<meta charset="utf-8" />

The problem may be in the fetching of the data by PHP from MySQL since it seems fine in MySQL ones viewed via phpMyAdmin. So here is the code used to fetch this data from MySQL. Unless encoding has to be included in this file.

view.php (fetches needed data from MySQL may be a cause of encoding problem returning ???)

$q = mysql_query("SELECT * FROM `notice` WHERE nid = '".$nid."'");
         $a = mysql_fetch_array($q);

             $nid = stripslashes($a['nid']);
             $note = stripslashes($a['note']);
             $type = stripslashes($a['type']);
             $private = stripslashes($a['private']);
             $date = stripslashes($a['date']);
             $author = stripslashes($a['author'])

;

PS. Edited to be more clear.

PT Desu
  • 693
  • 4
  • 13
  • 26
  • a link to the page? or post some of the page source? – jcomeau_ictx May 15 '11 at 06:12
  • Declaring the character set is not enough. The data actually needs to *be* in that character set. Is it? – Pekka May 15 '11 at 06:13
  • apologies. I meant the page source as shown by your browser; as rendered by the PHP code. – jcomeau_ictx May 15 '11 at 06:16
  • how the code you are using to fetch the data – Pekka May 15 '11 at 06:24
  • The fact that you need to call stripslashes() tells me something is inherently wrong with your application. Are you sure you're following a consistent scheme for escaping strings in SQL? – MarkR May 15 '11 at 06:57
  • For this, and other, problems with utf8, see https://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored – Rick James May 28 '18 at 20:52

4 Answers4

5

You most likely need to set the character set of your database connection to UTF-8.

How to do that depends on the database library you are using.

In the old mySQL library, it would be

mysql_query("SET NAMES utf8");

(pre mySQL 5.0.7) and

mysql_set_charset("utf8");

for mySQL 5.0.7 and newer.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
0

try calling

mysql_query("SET NAMES utf8");

somewhere before your query. of course if you're on PHP.

Headshota
  • 21,021
  • 11
  • 61
  • 82
  • tried and still ??? so ye not working as well as meta utf so have to look deeper in why this is happening – PT Desu May 15 '11 at 06:23
  • @PT Desu: Maybe the data in the database now is corrupted because you didn't use this when inserting? Is the data ok or ?? in phpMyAdmin or other tools? – nitro2k01 May 15 '11 at 07:00
  • Oddly yes, it is displayed in Kanji in phpMyAdmin if I would to view the specific row. Any language works perfectly in phpMyAdmin, be it German, Russian, Japanese or others but the moment I try to output it onto the page truth a PHP call to MySQL it returns ??? which is not a browser problem as I would have seen blocks rather then "?" which hits the fact its PHP fetching the data that is creating this problem somewhere. – PT Desu May 15 '11 at 18:27
-1

See: http://www.w3.org/TR/html4/charset.html#h-5.2.2

So you should use: <META http-equiv="Content-Type" content="text/html; charset=UTF-8">

Ideally this info shall be supplied in headers of HTTP response.

c-smile
  • 26,734
  • 7
  • 59
  • 86
  • 1
    1) OP's declaration is valid HTML5 and generally just works. 2) The ?? bug is a sign of DB problems; browserside charset problems usually manifest themselves differently. (With "random" garbled characters rather than question marks.) – nitro2k01 May 15 '11 at 06:23
  • not browser problem as it works perfectly with Japanese sites with full Kanji support and other lang support. In DB the Author name is written in Kanji but shown as ??? ones called – PT Desu May 15 '11 at 06:25
  • 1
    To PT Desu on "that did not work originally" what exactly "that" and "originally". You did so many controversial edits in your question that it is really hard to shoot that moving target. – c-smile May 15 '11 at 07:12
-1

mysql utf-8 is not the real utf-8. it uses 3 bytes instead of 4. For real utf-8, you have to use utf8mb4:

mysql_query("SET NAMES utf8mb4");
mysql_set_charset("utf8mb4");
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Jan
  • 11
  • 3