7

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?

Rick James
  • 135,179
  • 13
  • 127
  • 222
PleaseHelpMe
  • 709
  • 3
  • 8
  • 13
  • 1
    do you have a particular reason to not store the 'E' and then represent that as a euro symbol on screen? In text you sometimes cannot avoid storing special characters, but when I don't have to, and it's critical that they don't change into something else to make sure validations don't go wrong, I'd rather solve it by storing the 'E'... – Whakkee May 11 '11 at 19:36
  • 3
    If you're going to store "E" rather than the symbol, you might as well just adhere to standards and use the ISO currency code (e.g. "EUR")... And as long as we're doing that, letting your presentation logic turn a currency code into a symbol is probably a better design decision, anyway. :) – Dan J May 11 '11 at 20:24
  • Just do the following: In `php.ini` set `default_charset = "utf-8"` The above will change the default character set for PHP to utf-8. Also change the character set for Apache in `httpd.conf` as below: `AddDefaultCharset UTF-8` Then restart Apache and hopefully your problem will be resolved. Remember that your database character set should also be utf8. – Imran Zahoor May 13 '15 at 07:30

3 Answers3

15

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");
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
1

"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

Tamler
  • 130
  • 1
  • 8
  • This link is no longer working. Would've been better if you could've posted the relevant info right in your answer. – Vincent Apr 06 '20 at 16:56
-4

You can convert it to the HTML code of €

Brds
  • 1,035
  • 3
  • 17
  • 37
  • 2
    To explain the downvotes: Using HTML entities is not regarded good practice any more in 2011. If you get the encoding right in every step of the process (something that is a *must* anyway), there will be no need for HTML entities, even if they seem to solve the problem at hand. – Pekka May 13 '11 at 18:46