我的项目有点问题。事实证明,当名字或姓氏的值中包含“ñ”时,查询结果不会返回任何内容 这是我的代码
的config.php:
<?php
ini_set('mssql.charset', 'UTF-8');
header('content-type: text/html; charset=utf-8');
$serverName = "192.168.1.21"; /* IP add of db Server */
$connection = array("Database" => "db", "UID" => "?", "PWD" => "***");
$conn = sqlsrv_connect($serverName, $connection);
if(!$conn){
echo "Connection could not be established.";
die(print_r(sqlsrv_errors(), true));
}
?>
更改为MyQuery:
$idnumber = $_POST["idnum"];
$response = array();
$query2 = "SELECT cLName, cFName, cMName, cQualifier FROM student WHERE cIDNO = '$idnumber'";
try{
$stmt2 = sqlsrv_query($conn, $query2);
}
catch(PDOExeption $ex){
$response["Success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}
$row2 = sqlsrv_fetch_array($stmt2, SQLSRV_FETCH_ASSOC);
if($row2){
$response["Success"] = 1;
$response["message"] = "Data available";
$last_name = $row2["cLName"];
$first_name = $row2["cFName"];
$qualify = $row2["cQualifier"];
$first_name = html_entity_decode(htmlentities($first_name));
$last_name = html_entity_decode(htmlentities($last_name));
$name = $first_name." ".$last_name." ".$qualify;
$response["Name"] = $name;
echo json_encode($response);
}
else{
$response["Success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}
但我不知道为什么这会发生在我的程序中。
数据库中的示例: cLName = "Española", cFName = "Edgar", cQualifier = "Jr."
输出 "Name":"Edgar Jr."
同时: cLName = "Agustin", cFName = "Florence", cQualifier = "Jr."
输出 "Name":"Florence Agustin Jr."
想法?请?
注意:我无权更改或更改数据库中的任何内容。
答案 0 :(得分:1)
我认为它的编码错误mssql conest不是UTF-8模式,
如果以下是ISO-8859-1的内存,我做了什么,并且utf8_encode
缺少输出是OP得到的,但是utf8_encode
是正确的
<?php
$row = ['cLName' => "Española", 'cFName' => "Edgar", 'cQualifier' => "Jr."];
$last_name = utf8_encode($row["cLName"]);
$first_name = $row["cFName"];
$qualify = $row["cQualifier"];
$first_name = html_entity_decode(htmlentities($first_name));
$last_name = html_entity_decode(htmlentities($last_name));
$name = $first_name." ".$last_name." ".$qualify;
$response["Name"] = $name;
$enc = json_encode($response,JSON_UNESCAPED_UNICODE);
print_r($name);
print_r($response);
print_r($enc);