繁体   English   中英

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

[英]UTF-8 encoding issue with turkish characters in php, ajax, json, jquery ,mysql

我在字符编码方面遇到麻烦。 我尝试了很多事情,但没有结果。 就是这种情况:我的数据库中有一些存储过程,并且向用户发送通知。 这里是:

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. 没有土耳其语字符,没有问题:

    情况1

  2. 一些土耳其语字符:ö,ç,ü。 我在控制台中收到此消息:

    (索引):167未捕获的TypeError:无法读取null的属性'counter'

    在这里,ajax无法通过json_encode()从背面的php页面(fetchMessages.php)中获取变量。

  3. 其余的土耳其字符:ş,ı,ğ。

    情况3

这是我的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页面中看到一些注释。 我也尝试过 但正如我所说,没有结果。

并且,如果我将它们从“不带BOM的utf-8”转换为“ utf-8”,则只会收到上述情况2中字符的警报对话框:

[object XMLHttpRequest]

但是,剩下的我还是问号。

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

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

您可以尝试添加

"SET NAMES UTF-8" 

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

暂无
暂无

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

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