Thursday, 22 August 2013

PHP and mysql charset issue

PHP and mysql charset issue

Ok, hang on with me. I am really bad at this. I have received little help
from friend who knows some coding but is not available atm.
I have mysql database which contains data stored in UTF8_general_ci
Then in wordpress I have page which calls file number 1, haku.php.
Contents below:
<?php
$result = mysql_query("SELECT DISTINCT jalleenmyyja_postitp FROM
jalleenmyyjat order by jalleenmyyja_postitp");
while($r = mysql_fetch_array($result)){
if ($r["jalleenmyyja_postitp"] != "")echo '<option
value="'.$r["jalleenmyyja_postitp"] .'">'. $r["jalleenmyyja_postitp"]
.'</option>';
}
?>
Those are then put inside dropdown menu. So far so good. Then when some
selection is done in dropdown, it should return results below which it
does by file number 2 which is JS below:
function haeyritys(x){
jQuery.ajax({
type: "POST",
url: "/yrityshaku.php",
data: "kaupunki=" + x,
success: function(data){
document.getElementById('selResult').innerHTML=data;
}
});
}
Then after this the file number 3. returns the data from database.
if($_REQUEST["kaupunki"] !=""){
$con = mysql_connect($host, $user, $pass);
mysql_select_db($db_name);
$result = mysql_query("select * from jalleenmyyjat where
jalleenmyyja_postitp = '" . utf8_decode($_REQUEST["kaupunki"]) ."'
order by jalleenmyyja_nimi");
if (mysql_num_rows($result) == 0){
echo '<div style="border-bottom:1px solid
#ccc;padding:5px;"><b>Ei tuloksia...</b>';
}else{
while($r = mysql_fetch_array($result)){
if ($r["google"] != ""){echo '<a
onclick="location.href=\'#top\'" href="'. $r["google"] .'"
target="map">';}else{echo '<a onclick="location.href=\'#top\'"
href="/eikarttaa.html" target="map">';}
echo '<div style="border-bottom:1px solid
#ccc;padding:5px;"><b>' . $r["jalleenmyyja_nimi"] . '</b>';
if ($r["jalleenmyyja_osoite"] != "")echo '<br>' .
$r["jalleenmyyja_osoite"];
if ($r["jalleenmyyja_postino"] !="" ||
$r["jalleenmyyja_postitp"] !="")echo "<br/>" .
$r["jalleenmyyja_postino"] . " ". $r["jalleenmyyja_postitp"];
if ($r["jalleenmyyja_puh"] != "") echo "<br/>Puh: ".
$r["jalleenmyyja_puh"];
if ($r["www"] != "")echo '<br/><a target="_blank"
href="'.$r["www"].'">Verkkosivuille &raquo;</a>';
echo '</div>';
echo '</a>';
}
}
mysql_close($con);
}
?>
However, the foreign characters are question marks. If I add
mysql_set_charset('utf8'); to file number 3 which opens the database
connection, the results show symbols correctly but the data in dropdown
which could have the symbol, does not return results! so it's one way or
the other.

No comments:

Post a Comment