简体   繁体   English

PHP和MySQL字符集问题

[英]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. 我的朋友几乎没有帮助,他们知道一些编码,但是无法使用atm。

I have mysql database which contains data stored in UTF8_general_ci 我有mysql数据库,其中包含存储在UTF8_general_ci中的数据

Then in wordpress I have page which calls file number 1, haku.php . 然后在wordpress中,我有一个页面调用文件号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: 然后,当在下拉列表中进行某些选择时,它应该返回结果,低于该结果时,它的文件号为2,即下面的JS:

    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. 然后,此文件号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);



}

?>

However, the foreign characters are question marks. 但是,外来字符是问号。 If I add mysql_set_charset('utf8'); 如果我添加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! 到打开数据库连接的文件编号3时,结果正确显示符号,但下拉菜单中可能包含该符号的数据不会返回结果! so it's one way or the other. 所以这是一种方法。

If you see question marks in place of characters on a web page, chances are the browser does not know which charset is the correct one. 如果您在网页上看到问号代替字符,则可能是浏览器不知道哪个字符集是正确的。 You can tell the browser which charset to use by one of the following methods: 您可以通过以下方法之一告诉浏览器使用哪个字符集:

HTML5: HTML5:

<meta charset="UTF-8">

HTML4: HTML4:

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

XHTML 1.x: XHTML 1.x:

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

XHTML 1.x as XML: XHTML 1.x作为XML:

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

The meta tags belong in the head of the html document, and should be placed near the top (between <head> and </head>). 元标记位于html文档的开头,应放在顶部附近(在<head>和</ head>之间)。 If unsure about which one to use, use html4 for supporting old browsers, html5 if support for modern browsers is sufficient. 如果不确定要使用哪一种,请使用html4支持旧的浏览器,如果支持现代的浏览器就使用html5。

Source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM