如何使用PHP在MySQL中存储“€"符号? [英] How can I store the '€' symbol in MySQL using PHP?

查看:132
本文介绍了如何使用PHP在MySQL中存储“€"符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将PHP字符集设置为utf-8:

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

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

我已使用排序规则utf8_unicode_ci将MySQL中的currency列设置为varchar(1).

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

但是,PHP中的以下代码:

However, the following code in PHP:

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

在MySQL中产生以下字符:

Produces the following character in MySQL:

â

如何获取符号以正确存储在MySQL中?

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

推荐答案

请确保您的脚本文件,即包含该行的文件

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

$currency = '€';

是编码的UTF-8.您应该在编辑器或IDE的另存为"对话框中选择一种效果.

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

然后确保您的连接也采用UTF-8编码-默认情况下为ISO-8859-1.

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

连接到数据库后,对于之前 5.0.7:

After connecting to the database, for mySQL before 5.0.7:

mysql_query("SET NAMES utf8");

对于mySQL 5.0.7和更高版本:

For mySQL 5.0.7 and newer:

mysql_set_charset("utf8");

这篇关于如何使用PHP在MySQL中存储“€"符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆