0

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.

Joshxtothe4
  • 4,061
  • 10
  • 53
  • 83
  • this has been asked before! check here: http://stackoverflow.com/questions/346531/utf8-problem-with-mysql-5/346552#346552 – markus Apr 21 '09 at 11:26

1 Answers1

0

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
  • 13,282
  • 4
  • 35
  • 52