Tutorial :How to display a mysql table data in another language properly in php



Question:

i have a mySQL table which the data in one of its columns is in a language other than English.(Persian) when i input data in the table , it is shown properly but when i want to show the data in a php file it displays like this:???? well,what should i do in order to show the data in its correct form?


Solution:1

As I've worked a lot with "non-english" characters, several things are required for proper display and storage of those characters.

In no particular order (as I don't know what charset is best suited for Persian, I'll use UTF-8, if it's different, you just use the one you need):

Tell your browser what charset you are using, either by setting the proper header from PHP header('Content-type: text/html; charset=utf-8'); or set the meta tag in your html like so: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

In the database avoid mixing different collations and charsets in the columns/tables. I always set the database, the tables and the columns to utf8_general_ci which for my needs work all the time (languages like English, German, Serbian, Hungarian...).

As Jan said, read http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html You'll most likely need to execute query something like SET NAMES utf8 right after connecting to the database.

All this should ensure the proper displaying of unicode characters. However, there is one more thing that can override all this - the web server. Apache (don't know about the other servers) has a AddDefaultCharset directive. On most setups this is left as Off, but I did came across setups where the default charset was set to latin1, thus overriding all my charset settings. If this is set, it is set in the httpd.conf (or similar configuration file). If you have access to it, I recommend setting it to Off. If you don't, then you can override the global value with .htaccess placed in your webroot, with something like: AddDefaultCharset utf-8


Solution:2

Add the correct content-type header:

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

or whatever encoding you use instead of UTF-8


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »