解决方法:
在$con = new mysqli(...); 的后面加上 mysqli_query($con, "set names utf8");
在前面加没有用。
<?php
// header("content-type:text/html; charset=utf-8");
$servername = "localhost";
$username = "root";
$password = "123456";
$dbname = "test1";
$con = new mysqli($servername,$username, $password, $dbname);
// 加入此句代码可解决乱码问题,乱码中文会以问号代替。
// 此句代码要写在new mysqli(...)的后面,写在前面无用。
mysqli_query($con, "set names utf8");
if($con){
echo "成功!";
}
$sql = "select * from stu";
$return = $con->query($sql);
while($row = $return -> fetch_assoc()){
echo $row["name"];
}
?>