我正在尝试从sql server数据库中获取一些数据并在json中打印,但数据库中的某些数据是UTF-8(阿拉伯语)。我在网上搜索并尝试了很多解决方案,但没有一个能为我工作,任何帮助都会受到赞赏。这是我的代码:
<?php
$serverName = "127.0.0.1"; //serverName\instanceName
$connectionInfo = array( "Database"=>"filmnak_com_", "CharacterSet" => "UTF-8", "UID"=>"filmnak", "PWD"=>"G5j^wk44");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$sql = "SELECT CAST(ID AS INT) AS ID, CAST(Film AS TEXT) AS Film ,CAST(Name AS TEXT) Name FROM dbo.Movies";
$result = sqlsrv_query( $conn, $sql);
if( $result === false ) {
die( print_r( sqlsrv_errors(), true));
}
// looping through all results
// products node
$response["Info"] = array();
while ($row = sqlsrv_fetch_array($result)) {
// temp user array
$Info = array();
$Info["ID"] = $row["ID"] ;
$Info["Film"] = $row["Film"];
$Info["Name"] = utf8_decode($row["Name"]);
echo $row["Name"];
// push single product into final response array
array_push($response["Info"], $Info);
}
echo json_encode($response , JSON_UNESCAPED_UNICODE);
sqlsrv_free_stmt($result);
?>
浏览器中的结果:
???? ????1+1?????{"Info":[{"ID":8,"Film":"8O9","Name":"???? ????"},{"ID":9,"Film":"9O3","Name":"1+1"},{"ID":10,"Film":"10O5","Name":"?????"}]}
答案 0 :(得分:2)
使用此代码
$serverName="(local)";
$connetionInfo = array("DataBase"=>"your db","UID"=>"username","PWD"=>"pass",
"CharacterSet" => "UTF-8");<---------------this
$this->dblink=sqlsrv_connect($serverName,$connetionInfo);