我mysqli
在 php 代码中使用连接到数据库,我喜欢它的 OOP 功能。数据库包含Persian
语言中的数据,并且排序规则设置为utf8_persian_ci
.
创建连接后,charset
设置为utf8
:
<?php
class DBHandler {
private $DB_HOST = "localhost"; //localhost
private $DB_USER = "------"; //root
private $DB_PASS = "------"; // ""
private $DB_NAME = "------"; //test
private $mysqli;
private $username;
public function __construct($user) {
$this->username = $user;
// CONNECT TO THE DATABASE
$this->mysqli = new mysqli($this->DB_HOST, $this->DB_USER, $this->DB_PASS, $this->DB_NAME);
if (mysqli_connect_errno()) {
throw new Exception("Unable to connect to the database. Error number: " . $this->mysqli->connect_errno);
}
$this->mysqli->set_charset("uft8");
}
public function __destruct() {
//We are done with the database. Close the connection handle.
$this->mysqli->close();
}
}
我期待以正确的编码检索数据,但在查询数据库后,所有字符都变成了?
.
我只是mysqli
用函数替换了所有实例,mysql
一切正常。
我想知道是什么原因造成的,我该怎么做才能使用mysqli
函数并以正确的编码获取数据?