Working with PHP and MySQL. When i save a string in the db, on retrieving it later on, it pulls out some funny characters instead of the double and single quotes in the original string. Some of those funny characters are â€
for the double quotes, ’
for the single quotes and –
for the dashes. htmlspecialchars()
, htmlentities
and addslashes
don't seem to solve my problem. Kindly help.
Asked
Active
Viewed 657 times
0
-
What collation do you use for your database? Try `utf8_unicode_ci` or `utf8_general_ci`. – Antony Mar 28 '13 at 06:45
-
See http://stackoverflow.com/questions/2292004/getting-a-instead-of-an-apostrophe-in-php#2292073 . It appears to be an encoding issue. – Moshe Mar 28 '13 at 06:45
-
@Antony, `utf8_general_ci` – gthuo Mar 28 '13 at 06:52
-
@GThuo Did you open the MySQL connection with utf8? Like `SET NAMES utf8` or `charset=utf-8`. What functions do you use when you connect to the database? – Antony Mar 28 '13 at 06:55
-
I'm using PDO prepared statements.. – gthuo Mar 28 '13 at 06:59
-
@GThuo When you connect to the database, use `'mysql:dbname='.$db_name .';host='.$host.';charset=utf-8'` – Antony Mar 28 '13 at 09:00
-
@Antony, still no success :-( – gthuo Mar 29 '13 at 08:24
2 Answers
0
here i found utf8 tutoril which is help to solve prob.
example is give below
<form action="demo_form.asp" accept-charset="ISO-8859-1">

MageDev
- 239
- 2
- 14
0
Call this:
header("Content-Type: text/html; charset=UTF-8");
It is also necessary to remove all the current misguided hacks (htmlspecialchars()
, htmlentities
, addslashes
and especially accept-charset="ISO-8859-1"
) you have right now.
Recommended reading so that you understand why the above should work in your case:
- What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text
- Handling Unicode Front To Back In A Web App
After understanding this you should see the tell-tale signs of UTF-8 misinterpreted as Windows-1252 and Windows-1252 misinterpreted as UTF-8 from miles away.
To see that the header call is working correctly:

Esailija
- 138,174
- 23
- 272
- 326
-
@Esailijia, there is this HTML meta tag on that page: `` It serves the same purpose as the call above or? – gthuo Mar 29 '13 at 08:25
-
are you saying I should not use the functions above i.e. `htmlspecialchars, htmlentities` and `addslashes`? – gthuo Mar 29 '13 at 08:27
-
1@GThuo yes, if you are using them (htmlentities, addslashes just makes no sense in general), then `charset=utf-8` has no effect. If you are using htmlentities, make sure you pass `UTF-8` as encoding. – Esailija Mar 29 '13 at 11:04
-
1@GThuo No, the header has a priority. If your web server is currently sending a header with charset attribute, then the meta tag has no effect. Check the headers sent for your page. – Esailija Mar 29 '13 at 11:10