无法在MySQL表中插入♥字符

时间:2022-09-25 21:36:44

I am trying to insert a ♥ into a MySQL table with PHP. It comes from an input field.

我试图用PHP将♥插入MySQL表。它来自输入字段。

The table's charset is utf8_general_ci, and in PHP I use mysql_query("SET NAMES 'utf8'"); right after connection is made.

表的字符集是utf8_general_ci,在PHP中我使用mysql_query(“SET NAMES'utf8'”);在连接之后。

But the ♥ just becomes a '?' when inserted.

但是♥只是变成'?'插入时

I have also tried inserting ♥ into the table from phpMyAdmin, but it returns this error:

我也尝试从phpMyAdmin插入♥到表中,但它返回此错误:

Warning: #1366 Incorrect string value: '\xE2\x99\xA5' for column 'subject' at row 1

The result is also '?' instead of ♥.

结果也是'?'而不是♥。

Any ideas on what causes this?

有什么原因可以解释这个问题?

2 个解决方案

#1


23  

This is because of incompatible character set and collation defined on table's column.

这是因为在表的列上定义了不兼容的字符集和排序规则。

Try changing character set of your table or column to UTF8.

尝试将表或列的字符集更改为UTF8。

ALTER TABLE table_name CONVERT TO CHARACTER SET utf8;

or

要么

ALTER TABLE table_name MODIFY col VARCHAR(255) CHARACTER SET utf8; 

#2


0  

try this:

尝试这个:

ini_set('default_charset', 'utf-8');

in your php file and set the :

在你的php文件中并设置:

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

and chek in you data base for collation to utf8_general_ci

并在你的数据库中chek到utf8_general_ci

#1


23  

This is because of incompatible character set and collation defined on table's column.

这是因为在表的列上定义了不兼容的字符集和排序规则。

Try changing character set of your table or column to UTF8.

尝试将表或列的字符集更改为UTF8。

ALTER TABLE table_name CONVERT TO CHARACTER SET utf8;

or

要么

ALTER TABLE table_name MODIFY col VARCHAR(255) CHARACTER SET utf8; 

#2


0  

try this:

尝试这个:

ini_set('default_charset', 'utf-8');

in your php file and set the :

在你的php文件中并设置:

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

and chek in you data base for collation to utf8_general_ci

并在你的数据库中chek到utf8_general_ci