PHP连接SQLServer echo输出中文汉字显示乱码解决方法

1、查询结果有中文会显示乱码。

解决方法一(较简单,建议使用):

在php文件最开头写上:

header('Content-type: text/html; charset=UTF8'); // UTF8不行改成GBK试试,与你保存的格式匹配

2、查询前对SQL转码

$sql = "SELECT '是'='是'"; 
$sql=iconv('UTF-8','GBK',$sql);

3、查询结果对含中文列转码

$stmt = sqlsrv_query( $conn, $sql);
if($stmt){
    while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC) ) {
        echo iconv('GBK','UTF-8',$row[0])."
"; } }



    
    系统用户 | 访问记录
    


 





$uid, "PWD"=>$pwd, "Database"=>"DBAliSys");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn === false )  
{  
     echo "Could not connect.\n";  
     die( print_r( sqlsrv_errors(), true));  
}  
  

$tsql = "SELECT *  FROM sys_Loguser order by LoginTime desc";  
$stmt = sqlsrv_query( $conn, $tsql);  
if( $stmt === false)  
{  
     echo "Error in query preparation/execution.\n";  
     die( print_r( sqlsrv_errors(), true));  
}  
  

echo "";


while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))  
{  

echo "";
echo "";
//echo "";

echo iconv('GBK','UTF-8',"");

echo "";
echo "";
//echo "";
echo iconv('GBK','UTF-8',"");

echo "";
echo "";

/*
echo "";
echo "";
echo "";
echo "";
echo "";
*/
echo "";

}  
  
echo "
日志ID 用户名 客户端IP 服务器IP 星期 登录时间 退出时间
" . $row['LogID'] . "" . $row['UserName'] . "" . $row['UserName'] . "" . $row['ClientIP'] . "" . $row['ServerIP'] . "" . $row['Weeks'] . "" . $row['Weeks'] . "" . $row['LoginTime']->format('Y-m-d H:i:s') . "" . $row['ExitTime']->format('Y-m-d H:i:s') . "" ."(".$row['X'].",".$row['Y'].")"."" . $row['Browser'].$row['Browser_Version']. "" . $row['Language'] . "" . $row['Equipment'] . "" . $row['Brand'] . "
"; sqlsrv_free_stmt( $stmt); sqlsrv_close( $conn); ?>

PHP连接SQLServer echo输出中文汉字显示乱码解决方法_第1张图片

你可能感兴趣的:(php,开发语言)