1
votes

I am using Wamp server 2.1 on windows 7 with PHP 5.3.4, Apache 2.2.17, MySql 5.1.53.

httpd.conf: AddDefaultCharset "iso-8859-1"

php.ini: default_charset = "iso-8859-1"

I am using php mysqli class to insert data in a table with latin1 encoding, and column with latin1 encoding, latin1_swedish_ci collation. In the php file, I set charset as ISO-8859-1 via meta tag, I render a form with accept-charset="ISO-8859-1" attribute. I use mysqli::set_charset to set charset of the database connection to latin1, but still the data being entered is corrupted. As per my understanding, the data is valid latin1 characters.

The code:

$dbMain->set_charset('latin1');
$query = "INSERT INTO `table` (" . implode(',', array_keys($data)) . ") VALUES ('" . implode("','", array_values($data)) . "')";
$dbMain->query($query);

The data being corrupted: "the characters Ä, Ö, Ü, ä, ö, ü, and ß. Shop-Produkte für die Elektronik"

When I read the data through latin1 connection, it gives me garbage (black diamonds with question marks) instead of the data I inserted. When I read the data through utf8 connection, it shows me proper characters. Same in SqlYog (MySql client).. If I run "SET NAMES latin1" and browse the table, I see barbage characters. If I run "SET NAMES utf8" and browse the table, I see proper text. What's wrong here?

Update:

I ran the following queries in MySql client:

SET NAMES latin1;
SELECT my_column,HEX(my_column) FROM my_table;

and got this result

|my_column|HEX(my_column)|
--------------------------
|    ß    |     C39F     |    (entered manually from MySql client)
|   �    |       DF     |    (entered from php via latin1 connection)

Has my database gone cucu?

2
What I perceive is that the data is being inserted in utf8 encoding. But why?Adee
When I set names to latin1, it behaves as utf8 and when I set names to utf8, it behaves as latin1. Please helpAdee
Where does $data come from?Esailija

2 Answers

2
votes

A few things are important:

  • what encoding are you using while reading/viewing the data from data base?
  • how are you viewing your data from DB? through mySQL admin or some php script or html page?
  • what encoding is set in the browser then?
  • what encoding did you save your php file with?

Have you thought about encoding conversion? I want help you, because I spent some time about latin2 and utf8 encoding (polish language), but I need to have more information from You.

0
votes

Well! There was no problem with the values being saved, at all. In both cases, it was the retrieval that caused the problem.

In case of MySql client, the problem is that the client only shows the correct values being stored when I use utf8 connection as default connection of the client. May be the client's UI only works with utf8 encoding.

In case of HTML response, the PHP file's encoding was ISO-8859-1 but the .html template file was saved in UTF-8 encoding. The website is using a conventional parser to parse the template, so when it got the content from the UTF-8 file and parsed, the ISO-8859-1 data became busted. Changing the encoding of .html file to ISO-8859-1 solved the problem.

Thank you all for helping me out. I really appreciate that. I hope you don't get angry