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
Author by
PleaseHelpMe
Updated on July 19, 2022Comments
-
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 tovarchar(1)
with collationutf8_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 almost 13 yearsdo 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 almost 13 yearsIf 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 almost 9 yearsJust do the following: In
php.ini
setdefault_charset = "utf-8"
The above will change the default character set for PHP to utf-8. Also change the character set for Apache inhttpd.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 almost 13 yearsTo 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 about 4 yearsThis link is no longer working. Would've been better if you could've posted the relevant info right in your answer.