如何从包含外语的mysql中获取数据?

vzgqcmou  于 2021-06-24  发布在  Mysql
关注(0)|答案(0)|浏览(212)

我在一个项目中工作,我在mysql中用外语存储数据。在数据库中,由于我已将排序规则设置为“utf8 general ci”,所以一切正常。我可以毫无问题地看它。但是,当我尝试将数据检索到php文档中时,它不起作用。我的代码是:

<?php

    $servername = "localhost";
$username = "root";
$password = "";
$dbname = "31stknowledge";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

mysql_set_charset('utf8');
$sql = "SELECT * FROM course";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $Course = $row["Course"];
        $Duration = $row["Duration"];
        $Type = $row["Type"];
        $Description = $row["Description"];
        $Image = $row["Image"];
        header("Content-type: text/html; charset=utf-8");
         echo "<div class='col-lg-6 portfolio-item'>";
          echo "<div class='card h-100'>";
            echo "<a href='#'><img class='card-img-top' src='$Image' alt=''></a>";
            echo "<div class='card-body'>";
              echo "<h4 class='card-title'>";
                echo "<a href='#'>$Course</a>";
              echo "</h4>";
              echo "<p class='card-text'>$Description</p><br>";
              echo "<p class='card-text'>$Type</p><br>";
              echo "<p class='card-text'>$Duration</p><br>";
            echo "</div>";
          echo "</div>";
        echo "</div>";

    }
} else {
    echo "0 results";
}
$conn->close();
?>

有人能指出我的错误并引导我走向正确的方向吗?提前谢谢。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题