Php to select data from the mysql database with chinese character

In summary: WarrenDo you have the appropriate fonts installed?"- WarrenYes because I can see other chinese character .- WarrenDo you have the appropriate fonts installed?- WarrenYes because I can see other chinese characters. I use php to select data from the mysql database with chinese character but when I echo it,it show ?. I am sure the encoding is correct but it still don't show the chinese character. I am using IE 6. Please help.- Are the mysql table and your PHP program using an appropriate data type to store the encoded Chinese characters?- The data type of mysql table is ok since i can view it through phpmyadmin but i don
  • #1
ngkamsengpeter
195
0
I use php to select data from the mysql database with chinese character but when I echo it ,it show ? . I am sure the encoding is correct but it still don't show the chinese character . I am using IE 6 . Please help .
 
Technology news on Phys.org
  • #2
Do you have the appropriate fonts installed?

- Warren
 
  • #3
chroot said:
Do you have the appropriate fonts installed?

- Warren

yes because I can see other chinese character .
 
  • #4
ngkamsengpeter said:
I use php to select data from the mysql database with chinese character but when I echo it ,it show ? . I am sure the encoding is correct but it still don't show the chinese character . I am using IE 6 . Please help .

Are the mysql table and your PHP program using an appropriate data type to store the encoded Chinese characters?
 
  • #5
gabee said:
Are the mysql table and your PHP program using an appropriate data type to store the encoded Chinese characters?
The data type of mysql table is ok since i can view it through phpmyadmin but i don't know wheter there is a problems with the php code . Below is the code I use :
$sql="select * from mytable where type='mytype' ";
$result=mysql_query($sql);
$name=mysql_result($result,0,"name");
echo $name;

Are there any problem with my code ?
 
  • #6
You might need to set a content-type header when you serve the webpage, to tell the browser what to do with the data.

Try inserting

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

into the HTML of the page, or put

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

before your PHP code.

Let us know if it works!
 
  • #7
What do you get in place of the character. A question mark/box/placeholder? Or two separate and apparently random characters?

The UTF-8 suggestion above will work in one case and not the other IIRC. Try other encodings if the above suggestion fails.
 
  • #8
Jheriko said:
What do you get in place of the character. A question mark/box/placeholder? Or two separate and apparently random characters?

The UTF-8 suggestion above will work in one case and not the other IIRC. Try other encodings if the above suggestion fails.
A few question marks .
I have tested gbk,big 5 and other encodings but it still not work . Please help.
 
  • #9
Try UTF-16/UCS-2 whatever...

The easiest solution is to understand which encoding it is using in the first place. You should be able to find out by examining your database more thoroughly.
 
  • #10
Jheriko said:
Try UTF-16/UCS-2 whatever...

The easiest solution is to understand which encoding it is using in the first place. You should be able to find out by examining your database more thoroughly.

I have examined my database and it use gbk_chinese_ci but I set encoding to gbk cannot solve the problem .How ?
 
  • #11
My suggestion is... since, undoubtedly, others have run into the same problem as you have before, you should search around on Google for people that have had issues with chinese characters in MySQL databases.

The biggest issue here is you've got a problem that could be occurring on one of three different depedencies. It could be a problem with how you're storing it (MySQL), what datatype you're throwing it into (PHP), or how you're displaying it (XHTML).

I'd suggest, if you can't find out the solution by Googling for other people having the same problem, you should first eliminate the layers the problem could be sitting on. From PHP, output it to a server-side text file (with the proper encoding format) instead of HTML. If that does not work, you know the problem is in either PHP or MySQL. Keep working your way up until you have excluded the problematic area. Then, if for instance, the problem happens to be with MySQL, search the documentation for support with storing Chinese encoded characters.

Cheers,
- Sane
 
  • #12
I have solved the problems ,I use "set character_connection_set=gbk,character_result_set=gbk" Anyway , thanks for yours help .
 
  • #13
Help Me Please

Hi,
I have this problem with utf-8 character-set. I can see characters in PHPMyAdmin correctly, but in php page show ? instead characters.
Server Variables and Setting:
http://www.dof.ir/mysql_server_variable.gif

please help me
 
Last edited by a moderator:
  • #14
alireza55627 said:
Hi,
I have this problem with utf-8 character-set. I can see characters in PHPMyAdmin correctly, but in php page show ? instead characters.
Server Variables and Setting:
http://www.dof.ir/mysql_server_variable.gif

please help me

you can try to run query below first before running any query :
"set character_set_connection=utf8,character_set_result= utf8"
 
Last edited by a moderator:
  • #15
I think there are a number of possibilities you can go for, which are:

1- check to see the integrated environment you use for php + mysql is supporting chinese or not (for instance xampp software supports a number of languages and can make your life easy. I have tried mysql for persian language through xampp and it works fine).

2- If you want to use chinese characters using any php + mysql configuration, I guess you can do it, using the embeded encode and decode functions of php properly. I have stored an image into mysql database (sure, you know that mysql does not support picture formats) quite long time ago using encode and decode functions. Therefore, since garbage is garbage and the chinese characters may be presented in the garbage manner that an image is presented, you can encode your characters and view them back by your web browser upon decoding.

3- make sure your OS supports chinese fonts.
 
  • #16
ngkamsengpeter said:
The data type of mysql table is ok since i can view it through phpmyadmin but i don't know wheter there is a problems with the php code . Below is the code I use :
$sql="select * from mytable where type='mytype' ";
$result=mysql_query($sql);
$name=mysql_result($result,0,"name");
echo $name;

Are there any problem with my code ?

Does echo work with name? I thought you needed print_r.
 
  • #17
upon reading the first part of your reply I can conclude,

- If your phpadmin does not have any problem then you should not need any decode() or encode() mechanisms (thus, your brief code also shows that you are not using any).


Upon reaing the second part of your reply I can tell you that,

- you got problem with your code. First of all, do not give me your abstract code, give me the actual code you are using. Second, why are you retreiving the data using type as the key??!. Third, upon getting the $result you don't need the parameter "name" in order to access the index 0 of the arry, all you need to do is call the index zero of the arry. Fourth, when you are using some text (or data) content that your are not sure of its type or it is quite large (basically larger than 250 somthing character) use the "blob" type for your field.
 

What is the best way to select data from a MySQL database with Chinese characters using PHP?

The best way to select data from a MySQL database with Chinese characters using PHP is to set the character encoding to UTF-8 in both the database and the PHP script. This will ensure that the Chinese characters are properly stored and retrieved.

Why are my Chinese characters showing up as question marks or garbled text when selecting data from a MySQL database using PHP?

This is likely due to a mismatch in character encoding. Make sure that both the database and the PHP script are set to use UTF-8 encoding. Additionally, it's important to use the correct collation for the database tables, such as utf8_general_ci or utf8_unicode_ci.

Can I use PHP functions such as mysql_query() or mysqli_query() to select data from a MySQL database with Chinese characters?

Yes, you can use these functions to select data from a MySQL database with Chinese characters. Just make sure to specify the charset parameter as UTF-8 in the function.

How can I ensure that my PHP script will properly display Chinese characters retrieved from a MySQL database?

In addition to setting the character encoding to UTF-8, you should also use the PHP function mb_convert_encoding() to convert the retrieved data to the proper encoding for display. This will help to prevent any garbled or incorrect characters from appearing.

Is there a way to test if my PHP script is properly retrieving Chinese characters from a MySQL database?

Yes, you can use the PHP function mb_detect_encoding() to determine the encoding of the retrieved data. If it is UTF-8, then the Chinese characters should be displayed correctly. You can also use a tool like MySQL Workbench to view the data and make sure it is stored correctly in the database.

Similar threads

  • Programming and Computer Science
Replies
7
Views
395
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
21
Views
449
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
2
Replies
50
Views
4K
  • Programming and Computer Science
Replies
21
Views
5K
Back
Top