9

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

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

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

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

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?

Sibu
  • 4,609
  • 2
  • 26
  • 38
Trolley
  • 2,328
  • 2
  • 23
  • 28

2 Answers2

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.

ALTER TABLE table_name CONVERT TO CHARACTER SET utf8;

or

ALTER TABLE table_name MODIFY col VARCHAR(255) CHARACTER SET utf8; 
Omesh
  • 27,801
  • 6
  • 42
  • 51
  • Ohh! I just changed the charset of the table, not the columns... Changed the columns to utf8, and now everything works! Thanks! – Trolley Sep 12 '12 at 10:50
0

try this:

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

in your php file and set the :

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

and chek in you data base for collation to utf8_general_ci

Harshal
  • 3,562
  • 9
  • 36
  • 65