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

11,616

Solution 1

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");

Solution 2

"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

Share:
11,616
PleaseHelpMe
Author by

PleaseHelpMe

Updated on July 19, 2022

Comments

  • PleaseHelpMe
    PleaseHelpMe almost 2 years

    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?

    • Whakkee
      Whakkee almost 13 years
      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'...
    • Dan J
      Dan J almost 13 years
      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. :)
    • Imran Zahoor
      Imran Zahoor almost 9 years
      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.
  • Pekka
    Pekka almost 13 years
    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.
  • Vincent
    Vincent about 4 years
    This link is no longer working. Would've been better if you could've posted the relevant info right in your answer.