I have a website where users enter a salary into a field and then it gets entered into the MySQL database. If I enter £75k it should get entered into the database as £75k. However, it's getting entered as Â£75k.
The doctype on the page is utf8
<meta http-equiv="content-type" content="text/html; charset=utf-8">
The database field is also utf-8
job_salary varchar(200) utf8_general_ci Yes NULL
I have really run out of ideas what may be causing this. It must be something on the frontend, as i'm using the same code on other websites and all work fine.
You should set the charset using a header as well, since the meta tag is only used if a header does not exist.
Try to add:
header('Content-Type: text/html; charset=utf-8');
Seems I might have misunderstood the question, are you saying it get entered into the database as such?
If, have you checked that you are running the connection to the database as utf8?
You might want to try this before you run the query if not:
mysql_query('SET NAMES utf8') or die(mysql_error());
Thanks, that seems to have fixed it. However, I just noticed on the other sites (where it is working) that the £ is also stored as Â£ in the database - yet on the frontend when the information is displayed, only the £ symbol shows (which is correct). I am not using any utf-8 decoding when getting the results from the database either. No idea whats going on here...
Most probably the "incident" is a result of different server settings on the web sites.
Which has helped with the previous systems for example is setting the web server charset to UTF-8 or/and the database also doing the same automatically.
If your code is to be deployed on multiple systems that you do not have absolute control on the server settings, I would recommend setting the code up with both sending a charset header and always setting the database connection to the correct charset.
Just wondering, what software do you use to "see" the data stored in MySQL? Could be something wrong with in that software.
I know how to set the database connection with the correct charset, but how do you send a charset header? Is that in the header for the page? I'm not sure what header is being referred to. Thanks!
This topic is now closed. New replies are no longer allowed.