How can I store the '€' symbol in MySQL us

2020-02-01 18:52发布

I've set the PHP character set to utf-8:

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

I've set the currency column in MySQL to varchar(1) with collation utf8_unicode_ci.

However, the following code in PHP:

$currency = '€';
$sql = "UPDATE myTable SET currency = '$currency' WHERE user = '$user'";
mysql_query($sql);

Produces the following character in MySQL:

â

How can I get the symbol to store properly in MySQL?

3条回答
祖国的老花朵
2楼-- · 2020-02-01 19:38

Make sure your script file, the file that contains the line

$currency = '€';

is encoded UTF-8. You should have an option to that effect in your editor's or IDE's "Save as" dialog.

Then make sure your connection is UTF-8 encoded as well - it is ISO-8859-1 by default.

After connecting to the database, for mySQL before 5.0.7:

mysql_query("SET NAMES utf8");

For mySQL 5.0.7 and newer:

mysql_set_charset("utf8");
查看更多
老娘就宠你
3楼-- · 2020-02-01 19:42

"Working with UTF-8 on the Web" section on this page gives a good rundown: http://dev.mysql.com/tech-resources/articles/4.1/unicode.html

查看更多
forever°为你锁心
4楼-- · 2020-02-01 19:43

You can convert it to the HTML code of €

查看更多
登录 后发表回答