UTF-8编码问题与php,ajax,json,jquery,mysql中的土耳其语字符

时间:2016-09-02 12:55:27

标签: php jquery mysql ajax character-encoding

我遇到字符编码问题。我尝试了很多东西,却没有结果。 情况就是这样:我的数据库中有一些存储过程,我向用户发送通知。这是:

some calculations..
IF @howmanyknew=0 || @howmanyknew>=6 THEN
    SET @memberID=(SELECT userID FROM myDB.users WHERE ticketID=@ID);
    INSERT INTO notifications (senderID,receiverID,messageContent,isRead)VALUES(1,@memberID,"Some turkish characters like ö ç ş ğ ü ı İ",0);
END IF;

这是我的通知表的结构:

id (INT) (PRIMARY_KEY)
senderID (INT)
receiverID (INT)
messageContent (TEXT) utf-8_turkish_ci
isRead (INT)

添加此内容后,就像在facebook中一样,向用户显示通知球。当用户点击它时,他/她会看到以下消息:

  1. 没有土耳其人,没问题:

    Case1

  2. 一些土耳其人物:ö,ç,ü。我在控制台中收到了这条消息:

      

    (index):167 Uncaught TypeError:无法读取null

    的属性'counter'

    这里,ajax无法通过json_encode()

  3. 从后面的php页面(fetchMessages.php)获取变量
  4. 其余的土耳其人物:ş,ı,ğ。

    Case 3

  5. 以下是我的index.phpfetchMessage.php

    的相关部分

    重要提示:两者均使用“无BOM的UTF-8”进行编码

    的index.php:

    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <script>
    .
    .
    $.ajax({                
        type:"POST",
        url:"fetchMessages.php",
        data: '{"id":"'+id+'"}',
        dataType: "json",               
        success: function(returnedData) {   
            var n=returnedData.counter;
            $(".content").empty();                  
            for(i=0;i<n;i++)
            {
                $(".content").append("<span style='display:block;text-align:left;font-size:9px;font-style:italic;background:gray'>"+returnedData.nick[i]+":</span>");                           
                $(".content").append("<div class='divtext' contentEditable>"+returnedData.msg[i]+"</div>");
            }                           
            $(".content").fadeIn("slow");
        },
        error: function(returnedData){
            alert(returnedData);
        }
    });
    .
    .
    </script>
    

    fetchMessages.php:

    <?php
        //header('Content-Type: application/json; charset=iso-8859-1');
        //setlocale(LC_ALL, 'tr_TR');
        include("conn.php");
        //iconv('cp1252', 'utf-8', "\x80 and \x95")-> "\xe2\x82\xac and \xe2\x80\xa2";
        $input = file_get_contents('php://input');
        $result=json_decode($input);
        $id=$result->id;
        $result=dbquery("select * from notifications where receiverID=".$id." and isRead=0");
        $senders=array();
        $msgs=array();
        $ids=array();
        $counter=0;
        while ($row = $result->fetch_assoc()) 
        {
            $m=$row["messageContent"];
            //$m=iconv("UTF-8", "ISO-8859-1//TRANSLIT", $m), PHP_EOL;
            $msgs[$counter]=$m;
            $ids[$counter]=$row["id"];
            $senderID=$row["senderID"];
            $result=dbquery('select * from usertbl where userID='.$senderID.'');
            $s=$result->fetch_assoc();
            $senders[$counter]=$s["username"];      
            dbupdate("notifications", array("isRead"=>1), array("id"=>$ids[$counter]));
            $counter=$counter+1;
        }
        echo json_encode(array(msg=>$msgs,nick=>$senders,counter=>$counter,ids=>$ids));
    ?>
    

    您还可以在php页面中看到一些注释。我也试过了。但正如我所说,没有结果。

    AND 如果我将它们从“没有BOM的utf-8”转换为“utf-8”,我只会在上面的案例2中找到一个警告对话框:

    [object XMLHttpRequest]
    

    但是,我仍然会得到其余的问号。

2 个答案:

答案 0 :(得分:0)

您可以尝试使用以下命令强制执行连接:set-charset

http://php.net/manual/en/mysqli.set-charset.php

答案 1 :(得分:0)

您可以尝试添加

"SET NAMES UTF-8" 

刚连接数据库后的Sql语句。