Chinese Characters

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi. I need to store chinese characters and Pinyins in a mysql database
and then read them from php and using the GD library and
imagettftext() function to create an image based on a chinese ttf font
of the chinese characters.

I have tried setting the encoding in the database as UTF-8 and BIG5
but the characters display (both in MYSQL and when displayed  via PHP)
as question marks.

Can anyone help?

Re: Chinese Characters

Kimmo Laine wrote:
Quoted text here. Click to load it

Unless next_page.php generates PHP, the script with this include will
only get HTML.

Quoted text here. Click to load it


    if (isset($_GET['foo'])) {
      echo '<?php echo $_GET[\'foo\']; ?>';
    } else {
      echo '<?php echo \'Not available\'; ?>';

File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot

Re: Chinese Characters

Quoted text here. Click to load it

Yes I do. I am familiar with the use of imagettftext() function. Just
having problems with chinese characters. Guessing it is an encoding
issue but not sure where to start.

Re: Chinese Characters

Steve wrote:
Quoted text here. Click to load it
First of all, you need to inform all programs what encoding you use. If  
you use utf-8, you need to inform MySQL by sending the command:


directly after connection to the database (I know the command has a  
misleading name. Look it up in the manual).

Also, you need to inform the browser by sending a header like:

Content-Type: text/html;charset=utf-8

You can do that from php by using the header function, but it is  
probably easier to set this in php.ini (two settings: content-type and  

Some text may already in the database and you may not be sure in what  
encoding it is stored. To find out, use the mysqldump utility. You can  
then open a dump of the table that contains the texts with an  
encoding-aware editor.

PHP itself really doesn't care much what encoding is used, as it treats  
the strings as binary strings anyway. PHP itself does not show anything  
on your screen; that is left to the browser. The only thing you should  
be aware of in PHP is the fact that the encoding used has an impact on  
the length of the characters. In utf-8, characters may have different  
lengths. If you need to work with substrings, there are special string  
functions to deal with multibyte encodings.

Good luck!

Re: Chinese Characters

Quoted text here. Click to load it

Thanks very much for this advice.

My http.conf states that my default character set is UTF-8.

My php.ini has a default charset of utf-8 and a default mime-type of

I have executed the Set Names command before sending the query to

The output just gives me question marks.

I've obviously missed something. Incidentally, for testing purposes I
am copying and pasting chinese characters from an Excel File (and also
from Chinese sites online). When I paste them into MySQL (using
HeidiSQL) they appear as squares and then, when I move away from the
field, become question marks....

Any thoughts?

Re: Chinese Characters

Quoted text here. Click to load it

good, although it is probably not used for php files
Quoted text here. Click to load it

Even better. This causes php sites to rendered in utf-8.

Quoted text here. Click to load it

The characters may be stored in another encoding by your previous attempts.

Quoted text here. Click to load it

Yes: monitor what's going on. I am going to assume you have a  
development database on your local PC.
Reconfigure MySQL to switch on the "query logging". This means that all  
queries are written to a log file. Effectively, it allows you to see  
what MySQL has done.
For monitoring the log file, I can recommend the freeware version of  
BareTail (see ). It shows the log in real-time  
and you can set the encoding to utf-8.
When you have this monitor up and running, do your tests again.

The log file will tell you a lot:
- the connection number is present, so you can see if the SET NAMES  
command was executed in the same connection as the INSERT and SELECT  
commands. If it was not, the SET NAMES command was not effective.
- You see the actual INSERT end  SELECT commands. You will not see  
anything special about the select commands because the log file does not  
contain the results, but you do see what was inserted using the INSERT  

I can really recommend you to take a look at the output of the mysqldump  
utility. You can use it to dump the output of a query if you are afraid  
of dumping too much data. If the characters are wrong in the output of  
mysqldump, they are wrong in the database as well.

Furthermore, I don't know HeidiSql. You might have to set the encoding  
inside that program as well.

Good luck!

Re: Chinese Characters

Thanks for all your advice. I am now in a position where I can get the
chinese characters into MySQL successfully and read them from it with
PHP and display appropriately in the browser (the problem seemed to be
caused by multi-byte characters occupying more "length" in the table.
I had, naively, thought that a VARCHAR of length 3 would be enough to
contain 3 chinese characters. Not so.).

I am now experiencing problems with displaying the characters using

When I use the cyberbit.ttf font all characters (attempted thus far)
display perfectly.

But using fonts located on the internet I get different results
(font wcl-02 from )
(font wts11 from )

With only some of the characters appearing.

Is this likely to be an encoding/character set problem or is it simply
because those characters are not in the character set.

Many thanks, in anticipation


Re: Chinese Characters

Quoted text here. Click to load it

Gotta be careful with those TrueType (TTF) fonts.
They're not very web-friendly, and they only ever work just right on  
Microsoft Windows.

Quoted text here. Click to load it

Site Timeline