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

前端 未结 2 848
死守一世寂寞
死守一世寂寞 2021-01-28 10:14

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

相关标签:
2条回答
  • 2021-01-28 10:31

    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

    0 讨论(0)
  • 2021-01-28 10:39

    Add the correct content-type header:

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

    or whatever encoding you use instead of UTF-8

    0 讨论(0)
提交回复
热议问题