PHP不连接MySQL数据库时,显示中文是正常的,连接数据库时,显示出来的中文为?
我在PHP的开头已经加上了<html><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><...
我在PHP的开头已经加上了
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<h1>欢迎</h1>
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$que='set names "utf8"';
$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";
$result=mysqli_query($con,$sql)
// Numeric array
$row=mysqli_fetch_array($result,MYSQLI_NUM);
printf ("%s (%s)n",$row[0],$row[1]);
// Associative array
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
printf ("%s (%s)n",$row["Lastname"],$row["Age"]);
// Free result set
mysqli_free_result($result);
mysqli_close($con);
?>
</html>
欢迎和你好都能正常显示,一旦连接了数据库,就没法正常显示中文,显示的中文为“?”数据库里面也已经设置了ENGINE=InnoDB DEFAULT CHARSET=utf8;求大神帮忙解决一下这个中文显示问题,谢谢
header("content-Type: text/html; charset=utf8");
printf("你好!");
这个我忘了复制了,也加上了,放在$con=mysqli_connect("localhost","my_user","my_password","my_db");前面的 展开
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<h1>欢迎</h1>
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$que='set names "utf8"';
$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";
$result=mysqli_query($con,$sql)
// Numeric array
$row=mysqli_fetch_array($result,MYSQLI_NUM);
printf ("%s (%s)n",$row[0],$row[1]);
// Associative array
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
printf ("%s (%s)n",$row["Lastname"],$row["Age"]);
// Free result set
mysqli_free_result($result);
mysqli_close($con);
?>
</html>
欢迎和你好都能正常显示,一旦连接了数据库,就没法正常显示中文,显示的中文为“?”数据库里面也已经设置了ENGINE=InnoDB DEFAULT CHARSET=utf8;求大神帮忙解决一下这个中文显示问题,谢谢
header("content-Type: text/html; charset=utf8");
printf("你好!");
这个我忘了复制了,也加上了,放在$con=mysqli_connect("localhost","my_user","my_password","my_db");前面的 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询