views:

178

answers:

2
+1  Q: 

Character set woes

I have a small ajax application built with php.

Using phpMyAdmin I have set a mysql database to utf-8, and have imported a textfile containing utf-8 data into it.

This worked fine on a windows machine with easyphp, after adding character-set-server=utf8 and default-character-set=utf8 to the my.cnf file.

I have now tried to move this to a production server where I do not have access to the configuration file, and characters such as umlauts are not displayed.

Is there something that can be set in the php code(not the configuration file) to fix this, or some command I can give to mysql?

I tried ALTER DATABASE vweb_50 DEFAULT CHARACTER SET utf8 COLLATE utf8_bin with phpMyAdmin to try from utf8_general_ci but it made no difference.

+1  A: 

There's a couple of things to be done here, and I'm not sure I understand your problem correctly, but if I do, I think most of it can be addressed with the PHP functions utf8_encode and utf8_decode:

  • utf8_encode
    • Encodes an ISO-8859-1 string to UTF-8
  • utf8_decode
    • This function decodes data , assumed to be UTF-8 encoded, to ISO-8859-1.

Also, look into htmlentities if needed.

Dave
Can you check if I am using htmlentities in the correct way?
Joshxtothe4
Hi Josh, I don't think you are. You wanna use htmlentities or utf8_encode on the **$rows[] = $row;** line, but modified a bit so you can use it field per field. Or don't use it in that function, but once you're ready to display it.
Dave
Hi Dave, do you mean something like htmlentities($rows[] = $row); ?
Joshxtothe4
Hi Josh, what I meant was that in the line $rows[] = $row, the $row variable itself is an array, so you'd have to do something like: $out = new array(); foreach($row as $k => $v) { $out[$k] = htmlentities($v); } $rows[] = $out;
Dave
A: 

If you use utf-8 throughout the application (Which I would recommend), you don't need utf8_encode/decode functions. Have you checked which encoding your page is served in? Probably the server is configured to send iso-8859-1 as charset, while your test environment sends utf-8. Set the encoding with a Content-Type header:

header("Content-Type: text/html; charset=utf-8");
troelskn
The pages that produce the symbols are part of a bigger ajax application, not the main page. Setting the header for a "grabber" page does not solve anything.
Joshxtothe4
You need to make sure that you send the proper header from all your pages -- including those that you reach via xhr.
troelskn