mysql_real_escape_string and apostrophe problem
-
11-10-2019 - |
Question
so I've been using mysql_real_escape_string to insert data into my database...but then when I do that it replaces all apostrophes with the symbol ’
so instead of display it's, it's displaying it’s.......
is there a way to reconvert those ’'s into apostrophes when I read back from the database?
Solution
This is some character encoding issue in some way.
Because in UTF-8, ’
(U+2019) is encoded with 0xE28099. And that represents the characters â
(0xE2), €
(0x80), and ™
(0x99) in Windows-1252.
So it seems that you just forgot to specify your output character encoding properly so that the browser uses its default character encoding Windows-1252 instead.
OTHER TIPS
Apply mysql_set_charset
to UTF-8 in your PHP.
Ensure your database and application are using the same character encoding.
In binary, '
is identical in value to ’
, so you need to ensure the encoding is set the same.
In your PHP application, you can specify this with the following two lines.
define(‘DB_CHARSET’, ‘utf8′);
define(‘DB_COLLATE’, ”);
This sets your application to use UTF-8 character set, which you must ensure matches your database's collation, then your problem will go away.
You can programatically change the database character set with the mysql_set_charset
function.