PHP和mysql charset问题

时间:2013-08-22 13:11:42

标签: php mysql character-encoding

好的,请跟我一起。我真的很擅长这个。我收到了一些朋友的帮助,他知道一些编码但是没有atm。

我有mysql数据库,其中包含存储在UTF8_general_ci

中的数据

然后在wordpress中我有一个调用文件编号1 haku.php的页面。内容如下:

 <?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>';
}


?>

然后将它们放在下拉菜单中。到现在为止还挺好。然后,当在下拉列表中完成某些选择时,它应该返回结果,在该结果之下,文件编号为2,即下面的JS:

    function haeyritys(x){
jQuery.ajax({
  type: "POST",
  url: "/yrityshaku.php",
  data: "kaupunki=" + x,
  success: function(data){
    document.getElementById('selResult').innerHTML=data;
  }

});
}

然后在此之后,文件号3.从数据库返回数据。

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);



}

?>

但是,外国字符是问号。如果我将mysql_set_charset('utf8');添加到打开数据库连接的文件号3,结果会正确显示符号,但下拉列表中可能包含符号的数据不会返回结果!所以它是这样或那样的。

1 个答案:

答案 0 :(得分:0)

如果您在网页上看到问号代替字符,浏览器可能不知道哪个字符集是正确的。您可以通过以下方法之一告诉浏览器使用哪个charset:

HTML5:

<meta charset="UTF-8">

HTML4:

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

XHTML 1.x:

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />

XHTML 1.x as XML:

<?xml version="1.0" encoding="UTF-8"?>

元标记属于html文档的头部,应放在顶部附近(在&lt; head&gt;和&lt; / head&gt;之间)。如果不确定要使用哪一个,请使用html4来支持旧浏览器,如果支持现代浏览器就足够了html5。

Source

相关问题