<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>数据测试</title>
</head>
<?php
$link = mysqli_connect('localhost','root','','happy');
if (!$link) {
die('Could not connect to MySQL: ' . mysql_error());
}
// 解决中文乱码问题
$link->query("SET NAMES 'UTF8'");
$sql = "select * from subway limit 5";
$result = mysqli_query($link, $sql);
while($row = mysqli_fetch_array($result)){
echo $row['id'] . " : " .$row['code'] . " : " . $row['name'];
echo "<br>";
}
mysqli_close($link);
?>
</html>
乱码前是这样的:
1 : subwayLine1 : ??
2 : subwayLine1 : ???
3 : subwayLine1 : ???
4 : subwayLine1 : ????
5 : subwayLine1 : ????
解决中文乱码后结果是这样的:
1 : subwayLine1 : 莘庄
2 : subwayLine1 : 外环路
3 : subwayLine1 : 莲花路
4 : subwayLine1 : 锦江乐园
5 : subwayLine1 : 上海南站