1、查询结果有中文会显示乱码。
解决方法一(较简单,建议使用):
在php文件最开头写上:
header('Content-type: text/html; charset=UTF8'); // UTF8不行改成GBK试试,与你保存的格式匹配
<?php
header("content-type:text/html;charset=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])."<br />";} }
<!DOCTYPE html>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>系统用户 | 访问记录</title><style>table {border-collapse: collapse;font-family: Futura, Arial, sans-serif;}caption {font-size: larger;margin: 1em auto;}th, td {font-size: 12px;padding: .65em;}th {background: #555 nonerepeat scroll 0 0;border: 1px solid #777;color: #fff;}td {border: 1px solid#777;}th {background: #696969;color:#FFFFFF;}tbody tr:nth-child(odd) {background: #ccc;}</style>
</head>
<body></body>
</html><?php
header("content-type:text/html;charset=utf-8");
//header("content-type:text/html;charset=GBK"); //设置编码$serverName = "aliworks.cn,57468"; //数据库服务器地址
$uid = "test_db"; //数据库用户名
$pwd = "alibaba123456"; //数据库密码
$connectionInfo = array("UID"=>$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 "<table border='0'>
<tr>
<th>日志ID</th>
<th>用户名</th>
<th>客户端IP</th>
<th>服务器IP</th>
<th>星期</th>
<th>登录时间</th>
<th>退出时间</th></tr>";while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
{ echo "<tr>";
echo "<td>" . $row['LogID'] . "</td>";
//echo "<td>" . $row['UserName'] . "</td>";echo iconv('GBK','UTF-8',"<td>" . $row['UserName'] . "</td>");echo "<td>" . $row['ClientIP'] . "</td>";
echo "<td>" . $row['ServerIP'] . "</td>";
//echo "<td>" . $row['Weeks'] . "</td>";
echo iconv('GBK','UTF-8',"<td>" . $row['Weeks'] . "</td>");echo "<td>" . $row['LoginTime']->format('Y-m-d H:i:s') . "</td>";
echo "<td>" . $row['ExitTime']->format('Y-m-d H:i:s') . "</td>";/*
echo "<td>" ."(".$row['X'].",".$row['Y'].")"."</td>";
echo "<td>" . $row['Browser'].$row['Browser_Version']. "</td>";
echo "<td>" . $row['Language'] . "</td>";
echo "<td>" . $row['Equipment'] . "</td>";
echo "<td>" . $row['Brand'] . "</td>";
*/
echo "</tr>";} echo "</table>";
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>