I've some problems with special characters in PHP/MySQL (I don't know which one). Then, I've data stored in a database,using php. I ensured before storing data to add this code:

  header('Content-type:text/html; charset=utf-8');
  $link=mysqli_connect("host","user","","db") or die(mysqli_error($link));
  mysqli_query($link,"SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8';SET NAMES utf8");

I also ensured Apache uses the correct charset:

AddDefaultCharset utf-8

In addition I also tried to add on every html page the meta tag:

<meta http-equiv="content-type" content="text/html" charset="UTF-8">

Further, I saved all my php and html files in UTF-8 format. I noticed that if I write a character like ò in a page and display that page it is correctly displayed. If I get a string query from a database containing ò, it doesn't display correctly, but if I change the code to:

header('Content-type:text/html; charset=iso-8859-1');

the query retrieved from the DB is correctly displayed, a normal writing not. So,the problem is on mysql charset? How could it be if I setted it with the previous instructions?

有帮助吗?

解决方案

try to change your column where you store this date to utf8 also

here some codes you may use.

Change the character-set/collation (database):

ALTER DATABASE db_name DEFAULT CHARACTER SET utf8;

Change the character-set/collation (table):

ALTER TABLE tbl_name DEFAULT CHARACTER SET utf8;

Change the character-set/collation (columns):

ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8;

其他提示

Try using http://fi2.php.net/mysqli_set_charset function instead of the mysqli_query() you are using now.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top