如何在网页上显示存储在Mysql数据库中的孟加拉语数据

时间:2018-04-23 12:15:52

标签: php mysql internationalization

我正在制作CMS并且必须在此CMS上提交Bangla数据并显示Bangla数据,但是Bangla数据显示不正确,但是单击提交按钮以正确查看孟加拉语。这个问题将如何解决?

点击提交按钮之前:

enter image description here

点击提交按钮后:

enter image description here

我的代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
    </head>
    <body>
        <?php
                $servername = "localhost";
                $username = "root";
                $password = "";
                $db ="bangla";
                $con1 = new mysqli($servername, $username, $password);
                $db_create = "CREATE DATABASE IF NOT EXISTS bangla CHARACTER SET utf8 COLLATE utf8_general_ci";

                if ($con1->query($db_create) === TRUE) {
                    echo "Database 'bangla' created successfully<br>";
                } 
                else {
                      echo "Error creating database: " . $con1->error;
                }
                $con2 = new mysqli($servername, $username, $password, $db);
                $table_create = "CREATE TABLE IF NOT EXISTS user (
                    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                    name VARCHAR(30) NOT NULL,
                    father VARCHAR(30) NOT NULL,
                    village VARCHAR(30)
                )ENGINE=MyISAM DEFAULT CHARSET=utf8";

                if ($con2->query($table_create) === TRUE) {
                    echo "Table 'user' created successfully<br><br>";
                } 
                else {
                      echo "Error creating database: " . $con2->error;
                }

                if (isset($_POST['submit'])) {
                    mysqli_query($con2, "SET CHARACTER SET utf8");
                    mysqli_query($con2, "SET SESSION collation collation='utf8_general_ci'");
                    $name = $_POST["name"];
                    $father = $_POST["father"];
                    $village = $_POST["village"];
                    $query = "INSERT INTO user (id, name, father, village) 
                    VALUES (NULL, '$name', '$father', '$village')";
                    $run = mysqli_query($con2, $query);
                    if ($run) {echo "User created successfully. ";} 
                    else {echo "User created fail.";}
                }
            ?>  
            <form  method ="post" accept-charset="utf-8">
                <input type="text" name="name" placeholder="Name">
                <input type="text" name="father" placeholder="father name">
                <input type="text" name="village" placeholder="village name">
                <input type="submit" value="submit" name="submit">
              </form>
                <?php
                $sql = "SELECT name, father, village FROM user";
                $result = $con2->query($sql);
                if ($result->num_rows > 0) {
                    while($row = $result->fetch_assoc()) {
                        echo "Name:" . $row["name"]. " <br>Father name: " . $row["father"]."<br>Village name:". $row["village"]."<br><br>";
                    }
                } else {
                    echo "0 results";
                }
        ?>
    </body>
</html>

2 个答案:

答案 0 :(得分:0)

写入'head'标签   喜欢

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

答案 1 :(得分:0)

我通过使用

解决了相同的问题

$ con-> set_charset(“ utf8”);

我的配置文件:

    <?php

// Creating a database connection

$con = mysqli_connect("localhost", "root", "root", "bibahabd_datacollect");
if (!$con) {

    die("Database connection failed: " . mysqli_connect_error());
}
$con->set_charset("utf8");

// Selecting a database

$db = mysqli_select_db($con, "bibahabd_datacollect");
if (!$db) {
    die("Database selection failed: " . mysqli_connect_error());
}

?>