开发者

How to retrieve special Char from database?

开发者 https://www.devze.com 2023-03-18 14:08 出处:网络
I am trying to retrieve a special Greek character µ from a MySQL database but in HTML it is showing this �.

I am trying to retrieve a special Greek character µ from a MySQL database but in HTML it is showing this .

My HTML charset: content="text/html; charset=windows-1252"

In addition, after it retrieves the character it also need to be able to use the character to compare back in the database.

I've tried putting the HTML code in the database like μ then it is not able to compare bac开发者_StackOverflowk to the database because HTML turns it into µ.

How can I do this appropriately?


First, I would recommend using utf8 as your charset.

Start by switching the default for the database:

ALTER DATABASE db_name CHARACTER SET utf8;

Next, to switch your tables over to utf8 apply this SQL:

ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8;

which you will have to do for each table.

Finally, and quite unfortunately, you have to know convert all the character data columns that were made under the old charset (varchar, text, etc.). You have to do this by way of first casting to a BLOB so that the characters already stored are not mangled:

alter table tbl_name change col_name col_name LONGBLOB;
alter table tbl_name change col_name col_name LONGTEXT CHARACTER SET utf8;

Wordpress publishes a column type by column type guide on how to do this step: converting database charset columns.

Then, finally update the charset declaration in your xhtml (or without the ending / if it is html):

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

This process is arduous, but will convert the database. It is much easier to start a db in utf8.

This is all assuming MySQL 5, for which you can see the Mysql 5 ALTER TABLE reference.


Try this on your table containing the special chars (based off of this post):

ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8;


See Shelhamer's answer, but you also need to set the db connection to utf8. You can either use mysql_set_charset('utf8'); right after you connect to the DB. Alternatively run the following query after your're connected:

SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8';

however, i would still recommend you use htmlentities() when displaying your content on a website to make sure is's all valid HTML. Firefox tends to barf if you mix up the encoding.


in php use utf8_encode() before saving it to the database. It will solve the problem

0

精彩评论

暂无评论...
验证码 换一张
取 消