douyabu1528 2015-11-17 20:27
浏览 87
已采纳

POST到mysql时,希腊字符未正确编码

I have written a php script that I use it to upload data from a mobile (iOS) app to mysql server. The procedure works perfect and I can enter and get data from the server, but i have noticed when I post greek letters are being encoded wrongly ("Δέντρο" instead of Δέντρο). I have entered below on my php:

header('Content-Type: text/html; charset=utf-8');

but it still the data are not correct encoded. The words are not encoded correct even if I enter the data directly on the webpage instead posting them from app. The main code that I use is the following:

header('Content-Type: text/html; charset=utf-8');

require("../db/mySQL_dao.php");
    $config = parse_ini_file("../../../municipapp.ini");

$returnValue = array();

if (empty($_REQUEST["name"]) || empty($_REQUEST["lon"]) || empty($_REQUEST["lan"])) {
    $returnValue["status"]="400";
    $returnValue["message"]="Didn't get all necessary info";
    echo json_encode($returnValue);
    return;
}


$issueName = htmlentities($_REQUEST["name"]);
$issue = htmlentities($_REQUEST["issue"]);
$issueComments = htmlentities($_REQUEST["comments"]);
$issueLon = htmlentities($_REQUEST["lon"]);
$issueLan = htmlentities($_REQUEST["lan"]);
$issueUserID = htmlentities($_REQUEST["userID"]);
$issueIsItOk = htmlentities($_REQUEST["isitok"]);

// Generate secure password
    $salt = openssl_random_pseudo_bytes(16);
    $secured_password = sha1($password . $salt);

     $dbhost = trim($config["dbhost"]);
     $dbuser = trim($config["dbuser"]);
     $dbpassword = trim($config["dbpass"]);
     $dbname = trim($config["dbname"]);

    $dao = new mySQL_dao($dbhost, $dbuser, $dbpassword, $dbname);

$dao->openConnection();

// Register New Issue

$result = $dao->insertIssue($issueName, $issue, $issueComments, $issueLon, $issueLan, $issueUserID, $issueIsItOk);

if ($result) {
    $issueDetails = $dao->getIssueDetails($issueName);
    $returnValue["status"]="200";
    $returnValue["message"]="Successfully Inserted";
    $returnValue["issueId"]=$issueDetails["id"];
    $returnValue["issueName"]=$issueDetails["name"];
} else {
    $returnValue["status"]="400";
    $returnValue["message"]="Could not insert data";
}

$dao->closeConnection();

echo json_encode($returnValue);
  • 写回答

1条回答 默认 最新

  • dongxuan58311366668 2015-11-17 21:23
    关注

    Check Collation of your database table. It is good variant if your collation, for example, is utf_general_ci. And it is bad variant if collation is latin1_swedish_ci, for example.

    To check collation:

    SELECT table_collation FROM information_schema.tables WHERE table_name = 'your_table';
    

    To check charset:

    SELECT cc.character_set_name
    FROM information_schema.tables t,
    information_schema.collation_character_set_applicability cc
    WHERE t.table_collation = cc.collation_name
    AND t.table_name = 'your_table';
    

    To chacnge collation and character set:

    ALTER TABLE your_table
    CHARACTER SET utf8
    COLLATE utf8_general_ci;
    

    Also set charset and collation after connect:

    SET NAMES utf8;
    SET collation_connection = utf8_unicode_ci;
    

    And try to insert without using htmlentities() for processing data and use htmlspecialchars instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开