对于下面的代码,如何输出支持UTF-8的MySQL查询?
<?php
$servername = "###";
$username = "###";
$password = "###";
$dbname = "###";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT Column1, Column2, Column3 FROM table ORDER BY Column1 DESC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
echo '<table class="">';
echo "<tr><th>Column1</th><th>Column2</th></tr>";
while($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>";
echo '<a href="' . $row['Column1'] . '" target="_blank">' . $row['Column3'] . '</a>';
echo "</td><td>";
echo $row['Column2'];
echo "</td>";
}
} else {
echo "Error.";
}
echo "</table>";
mysqli_close($conn);
?>
清晰输出UTF-8字符的选项有哪些?我在这里和其他论坛上尝试了几种解决方案,但我放弃了。数据库排序规则为utf8_unicode_ci
答案 0 :(得分:1)
在$ conn = mysqli_connect(..)之后添加此代码
mysqli_query($conn, "SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");