【发布时间】:2013-11-23 13:07:41
【问题描述】:
我正在使用 mysql。当我做简单的查询SELECT * FROM table它返回Алексеевич。它应该显示俄语字母。我的数据库、表、列设置为utf8_general_ci。 php文件是没有bom的utf8。当我查询 set NAMES cp1251 时,它解决了问题,但是如果所有东西都在 utf 8 中,为什么会有 cp1251?
数据库连接
class Database {
public $user = 'root';
public $password = '';
function __construct() {
$this->connect();
}
function connect() {
try {
$this->dbh = new PDO('mysql:host=localhost;dbname=university;charset=utf8', $this->user, $this->password);
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
}
function selectQuery( $sql ) {
$this->stmt = $this->dbh->prepare($sql);
$this->stmt->execute();
}
function insertQuery( $sql ) {
$this->stmt = $this->dbh->prepare($sql);
$this->stmt->execute();
}
}
查询
$this->db = new Database();
$q = $this->db->selectQuery('SELECT * FROM students');
$data = $this->db->stmt->fetchAll($q);
【问题讨论】:
-
很高兴你在上一个问题后改用 PDO :)
-
-1 表示同一个问题问了两次
标签: php mysql database pdo character-encoding