Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I store the '€' symbol in MySQL using PHP? [duplicate]

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?

like image 633
PleaseHelpMe Avatar asked May 11 '11 19:05

PleaseHelpMe


People also ask

How do I save currency symbols in SQL database?

Solution 2. INSERT INTO dbo. currency (country, currency,code, symbol) VALUES ('India', 'Rupees', 'INR', N'₨'); Note: the text column has to be of type nvarchar or nchar.

How to insert Unicode characters in MySQL using PHP?

In order to insert Unicode characters in MySQL, you need to create a table with Unicode support, select the appropriate encoding/collation settings, and specify the charset in the MySQL connection. Then, you can proceed and employ PHP code to insert Unicode as you please.

How do I store and sign in MySQL?

You should store it as &amp; only if the field in the DB contains HTML (like <span class="bold">some text</span> &amp; more ).


2 Answers

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");
like image 95
Pekka Avatar answered Oct 07 '22 06:10

Pekka


"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

like image 1
Tamler Avatar answered Oct 07 '22 06:10

Tamler