Showing � instead of £

2019-03-25 07:39发布

问题:

the symbol � is showing instead of £ when pulling data from the mysql database

the field in question its collated to utf8_general_ci

I also have the <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> on the head tag of the page

Any ideas please?

回答1:

Use mysql_query("SET NAMES 'UTF8'"); before you perform any queries to the database.



回答2:

As others said you should SET NAMES 'UTF8' id it not the default charset. It is also possible that the wrong characters were inserted into the database.

Check the source if it is indeed in UTF-8 format, and was imported as UTF-8. (You can re-import the data after setting NAMES with the query above).

If they were entered thru an HTML form, check if the HTML page containing your data entry form has the correct meta elemnent too.

Also check the headers your HTTP server sends you (you can do this with the Firebug extension). You can set the default charset for your server in a .htaccess file containing:

AddDefaultCharset UTF-8


回答3:

If you're using PHP, the recommended way to set the character encoding is mysql_set_charset( ).

My initial setup for MySQL is:

<?php
$db = mysql_connect( ... );
mysql_set_charset( 'utf8', $db );

header('Content-Type: text/html; charset=UTF-8');
?>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

This way, I'm never ever having problems with encoding.



回答4:

Please read http://mysqldump.azundris.com/archives/60-Handling-character-sets.html

The short version: In MySQL string fields have a character set attached, and so have connections. The connection character set is the server-side representation of the client character set, it is what the server thinks the client is using.

The short way to set the connection character set is SET NAMES utf8, as mentioned in the comment above.

If the field character set is different from the connection character set, MySQL will try to convert the field to the connection character set. It will not try to do that if both are equal.

The way to debug that is, as shown in the article, to look at the field using the HEX() function, checking what the actual on-disk data is and if it is correct.

Then you connect to the database, having set your connection to the proper client character set with SET NAMES utf8, if you actually are working with utf8 as you claim. Then output the HEX() of the string again, it should be the same. If it is not, the connection does not have the right character set, for example, if you forgot to SET NAMES utf8.

You can configure the default connection character set in MySQL, using the definition of SET NAMES from the manual, http://dev.mysql.com/doc/refman/5.5/en/charset-connection.html

SET character_set_client = utf8;
SET character_set_results = utf8;
SET character_set_connection = utf8;

If you put this into the mysqld section of your my.cnf and restart the server, the SET NAMES should no longer be necessary, as the connection now by default will have utf8.



回答5:

this also happens if you get text with jquery and ajax, try echoing the string with utf8_encode($string)