views:

762

answers:

2

I have one database, with one table, with a particular field which has descriptions of various clothing. These descriptions often contain umlauts or similar characters.

I am retrieving this fields from two different php fields, and I am not doing anything to the data, yet it displays inconsistently.

In file1, it will display correctly as it is below, or adding

$con->query("SET NAMES 'utf8'");

and

header("Content-Type: text/html; charset=utf-8");

If I only add one of the above to file1, the data will not display correctly.

In file2, it will only display correctly if

$con->query("SET NAMES 'utf8'");

is set.

Adding either

header("Content-Type: text/html; charset=utf-8");

in union with the set names query, or leaving the set names query out will cause it to display incorrectly.

$con->set_charset("utf8");

seems to make no difference at any point.

This issue exists on internet explorer and firefox on windows and linux.

An example of the data which displays inconsistently:

ED HARDY SCHUHE IM JAPAN-STYLE, GRÖßE 36

File1:

<?php
header("Content-Type: text/html; charset=utf-8");
if (isset($_GET["pk"])) {
    $pk = $_GET["pk"];
}
$con = mysqli_connect("localhost","user","password", "database");
if (!$con) {
    echo "Can't connect to MySQL Server. Errorcode: %s\n". mysqli_connect_error();
    exit;
}
$con->query("SET NAMES 'utf8'");
$retreiveQuery = 'SELECT ARTICLE_NAME FROM AUCTIONS WHERE ARTICLE_NO = ?';
if ($getRecords = $con->prepare($retreiveQuery)) {
    $getRecords->bind_param("s", $pk);
    $getRecords->execute();
    $getRecords->bind_result($ARTICLE_NAME);
    while ($getRecords->fetch()) {
     echo "<h1>".$ARTICLE_NAME."</h1>";
    }
} else {
    print_r($con->error);
}

File2:

<?php
header("Content-Type: text/html; charset=utf-8");
if (isset($_GET["brand"])) {
    $brand = $_GET["brand"];
}
$con = mysqli_connect("localhost","user","pass", "database");
if (!$con) {
    echo "Can't connect to MySQL Server. Errorcode: %s\n". mysqli_connect_error();
    exit;
}
$con->query("SET NAMES 'utf8'");
$brand = '%' . $brand . '%';
$rows = getRowsByArticleSearch($brand);
echo "<table border='0' width='100%'><tr>" . "\n";
foreach ($rows as $row) {
    $pk = $row['ARTICLE_NO'];
    echo '<tr id="article_' . $pk . '">' . "\n";
    echo '<td><a href="#" onclick="updateByPk(\'Layer2\', \'' . $pk . '\', \'' . $title . '\', \'' . $pg . '\')">'.$row['ARTICLE_NAME'].'</a></td>' . "\n";
    echo '</tr>' . "\n";
}
echo "</table>\n";
function getRowsByArticleSearch($searchString) {
    $con = mysqli_connect("localhost","user","pass", "db");
    $recordsQuery = "SELECT ARTICLE_NAME FROM AUCTIONS WHERE lower(ARTICLE_NAME) LIKE ? and SUBCAT='' LIMIT 0,20";
    if ($getRecords = $con->prepare($recordsQuery)) {
     $getRecords->bind_param("s", $searchString);
     $getRecords->execute();
     $getRecords->bind_result($ARTICLE_NAME);
     $rows = array();
     while ($getRecords->fetch()) {
      $row = array(
                      'ARTICLE_NAME' => $ARTICLE_NAME, 
                  );
      $rows[] = $row;
     }
     return $rows;
    } else {
     print_r($con->error);
    }
}

I have an ajax applicaition, with index.php, for which I set

header("Content-Type: text/html; charset=utf-8");

I then load a layer with content via ajax using file2, which displays the data incorrectly. The data becomes a link, which uses file1 to load data into a different layer, which does display the data correctly.

I really don't understand where the inconsistency is coming from.

edit:

The data displays correctly in file1 with set names and the headers set, and the page shows as utf-8.

File 2 will only display the data correctly when the webpage information shows the charset to be ISO-8859-1, which means omitting set names and setting the header.

A: 

So the only difference is that file2 is used in combination with AJAX. Maybe you are setting some headers twice? Why are you connecting in both files? Don't you persist the database connection? Can it be, that you also have to influence the encoding of the AJAX request?

Similar problem: http://stackoverflow.com/questions/346531/utf8-problem-with-mysql-5/346552#346552

tharkun
My files are utf-8, the default encoding is utf-8, the connection is utf-8 as you can see above, all tables and columns have the right encoding and charset, and still this happens.
Joshxtothe4
I am using gedit, which saves as utf8 by default.
Joshxtothe4
file1 is used with ajax as well. They are used equally with ajax. index calls file1 in a layer which calls file 2 in a different layer. However in testing the above I am using them outside of ajax, and the problem is the same. I have not set headrers twice anywhere. I have not learnt about database connection persistence just yet...
Joshxtothe4
oh, I don't mean you should use persistent db connections per se. I meant why you don't factor your db connection out. but ok. If file 1 calls file 2 then there is a difference. what happens if you call file 2 directly in exactly the same manner as you would call file 1 from index?
tharkun
like that you're connecting twice per request while once would be enough!
tharkun
I have to revise a lot of code, I know..., anyhoo, if I call file2 from index it is the same as if it is called from file1. The problem is not do do with ajax, as it exists in an identical fashion when I call the files directly
Joshxtothe4
Where resides your updateByPk js function? Maybe that file is not encoded correctly?
tharkun
in a separate .js file, which is also utf8
Joshxtothe4
but the difference is in this output which goes through the js function, yes?
tharkun
not at the moment, when both files are just html
Joshxtothe4
I don't think I can help you. I don't get it and most probably I would need to clone your system to understand the problem :)
tharkun
A: 

Make sure that the files are saved in utf8 encoding, and not something else. This can probably be done through your IDE or text-editor. I.e. in eclipse you can choose Edit->Set Encoding

(My guess would be that file2.php or index.php is saved with an other encoding)

fredrik