Special Characters pulled from MySql Database

Hey guys,

I have a website that pulls info from a database. In the database, the data (which is user-entered) is often entered as curly quotes or long dashes. The result is, the database holds them as †or other similar gobbledy-gook.

When PHP displays this data on the web page, it still looks like funny characters. So, on the web page, something like “Farmer’s Market” will instead appear as “Farmer’s Market”.

This site was recently converted from ASP to PHP, and it was working fine in ASP, displaying everything properly, I believe using a particular function to convert the characters.

It seems that there should be a simple built-in function to do this in PHP, but I can’t seem to find the right one on PHP.net.

Any help is appreciated.

Thanks,
Louis

Make sure your page is served as utf-8, make sure your db is utf-8.

I’ve tried inserting:


header('content-type: text/html; charset: utf-8');

at the top of the PHP document.

I’ve tried inserting:

mysql_query('SET NAMES utf8');

after mysql_connect.

And my database lists each field’s “collation” as “utf8_general_ci”. And my HTML page has its content-type meta tag set to “text/html; charset=UTF-8”.

I also tried displaying the data using the utf8_decode() and utf8_encode() function. While they do give different results, they still display messed up characters, just different characters in each instance.

Thanks,
Louis

It seems that there should be a simple built-in function to do this in PHP

Yet, the solution is not to use any built in functions, either when displaying data and then entering it.
When data goes straight to database and straight from it, nothing bad can be occurred.
I suspect you do some unnecessary data preparation.

What happens if you enter this string in a form? What’s stored in db? What’s output to browser? What’s in post var?

Iñtërnâtiônàlizætiøn

I’m going to have to rethink this issue, because I think the problem is that the information is being manually entered into PHPMyAdmin from emails that are automatically sent. Unfortunately, the site is not inputting user info directly into the db, but instead the info is being emailed, then a moderator copies and pastes the info into fields in PHPMyAdmin.

I just assumed there was some way to pull those funny characters out and convert them in PHP. I’ll have to retest this once the form is set up to put the info directly into the DB.

Thanks for the help.

Louis