Question

I have a mysql database table to store country name and currency symbol - the CHARSET has correctly set to UTF8.

This is example data inserted into the table

insert into country ( country_name, currency_name, currency_code, currency_symbol) values 
('UK','Pounds','GBP','£');

When I look in the database - the pound symbol appears fine - but when I retrieve it from the database and display it on the website - a weird square symbol shows up with a question mark inside instead of the pound symbol.

Edit In my.cnf - the characterset was set to latin1 - I changed it to utf8 - then I Logged in as root and ran \s - it returned

Server characterset:    utf8
Client characterset:    utf8

Collations

-- Database
SELECT default_collation_name
  FROM information_schema.schemata
 WHERE schema_name = 'swipe_prod';

THIS DOES NOT RETURN ANYTHING

-- Table
SELECT table_collation
  FROM information_schema.tables
 WHERE TABLE_NAME = 'country';

THIS RETURNS utf8_general_ci

-- Columns
SELECT collation_name
  FROM information_schema.columns
 WHERE TABLE_NAME = 'country';

THIS RETURNS 7 ROWS but all have either null or utf8_general_ci

PHP CODE

<?php
$con = mysql_connect("localhost","user","123456");
mysql_select_db("swipe_db", $con);
$result = mysql_query("SELECT * FROM country where country_name='UK'");
while($row = mysql_fetch_array($result))
{
  echo $row['country_name'] . " " . $row['currency_symbol'];
}
mysql_close($con);
?>

Please advice Thanks

Was it helpful?

Solution

When you see that "weird square symbol with a question mark inside" otherwise known as the REPLACEMENT CHARACTER, that is usually an indicator that you have a byte in the range of 80-FF (128-255) and the system is trying to render it in UTF-8.

That entire byte-range is invalid for single-byte characters in UTF-8, but are all very common in western encodings such as ISO-8859-1.

When I view your page and manually switch the character encoding from UTF-8 to ISO-8859-1 (in Firefox with View >> Character Encoding >> Western (ISO-8859-1)) then the POUND SIGN displays properly.

So, what's wrong then? It's hard to say - there are dozens of places where this can be fouled up. But most likely it's at the database level. Setting the CHARSET on the table to UTF8 is generally not enough. All of your charsets and collations need to be in order before characters will move around the system properly. A common pitfall is improperly set connection charsets, so I'd start there.

Let me know if you need more guidance.

EDIT

To check what bytes are actually stored for that value, run this query.

SELECT hex( currency_symbol )
  FROM country
 WHERE country_name = 'UK'

If you see A3 then you know the character is stored as ISO-8859-1. This means the problem occurs during or before writing to the DB.

If you see C2A3 then you know the character is stored as UTF-8. This means the problem occurs after reading from the DB and before writing to the browser.

EDIT 2

-- Database
SELECT default_collation_name
  FROM information_schema.schemata
 WHERE schema_name = 'your_db_name';

-- Table
SELECT table_collation
  FROM information_schema.tables
 WHERE TABLE_NAME = 'country';

-- Columns
SELECT collation_name
  FROM information_schema.columns
 WHERE TABLE_NAME = 'country';

OTHER TIPS

Chipping in my 2 pence. I use utf8_encode() to output the data from the database. This seems to take care of other characters as well.

Try using

&pound;

Instead of the £ symbol, alternativly, where you print the £ sign, do:

<%=replace(stringWithPoundInIt,"£","&pound;"%>

I had the same problem. What I did was I ran this right after connecting to database (PHP):

mysql_query("SET NAMES utf8");

Hope this helps..

I had the same issue and I solved it by adding the below PHP code just before running the SELECT query on the MySQL database.

mysql_query("SET character_set_results=utf8", $link);

I ran into this issue myself and resolved at the output stage using "mb_convert_encoding".

Using your example...

echo $row['country_name'] . " " . mb_convert_encoding($row['currency_symbol'], "UTF-8");

You can check out here.I'm not sure if it works but give it a try because this is weird i've never seen something like it before.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top