[英]PHP - Fetching UTF-8 data from sqlsrv database
I'm Trying to fetch some data from sql server database and print in json , but some of the data in database is in UTF-8 (Arabic) .我正在尝试从 sql server 数据库中获取一些数据并在 json 中打印,但是数据库中的一些数据是 UTF-8 (Arabic) 。 I searched the web and tried many solutions , but none of them worked for me , any help would be appreciated .我在网上搜索并尝试了许多解决方案,但没有一个对我有用,任何帮助将不胜感激。 Here is my code :这是我的代码:
<?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);
?>
the result in browser :浏览器中的结果:
???? ????1+1?????{"Info":[{"ID":8,"Film":"8O9","Name":"???? ????"},{"ID":9,"Film":"9O3","Name":"1+1"},{"ID":10,"Film":"10O5","Name":"?????"}]}
USE THIS CODE 使用此代码
$serverName="(local)";
$connetionInfo = array("DataBase"=>"your db","UID"=>"username","PWD"=>"pass",
"CharacterSet" => "UTF-8");<---------------this
$this->dblink=sqlsrv_connect($serverName,$connetionInfo);
You'll need to specify the character set when connecting to the sqlsrv server.连接到 sqlsrv 服务器时,您需要指定字符集。 You can do this by adding the CharacterSet
parameter like this:您可以通过添加CharacterSet
参数来做到这一点,如下所示:
$serverName = "Your server name";
$connectionInfo = [ "Database"=>"your db", "CharacterSet" =>"UTF-8"];
$conn = sqlsrv_connect( $serverName, $connectionInfo);
将数据库排序规则设置为utf8_general_ci
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.