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 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端