【问题标题】:Invalid charsets in mysql [duplicate]mysql中的字符集无效[重复]
【发布时间】: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


【解决方案1】:

连接后使用此查询设置排序规则手册。

$PDO = new PDO('mysql:host = localhost; dbname = dbname', $username, $pswd);
$PDO -> exec("SET SESSION collation_connection = 'utf8_persian_ci';");
$PDO -> exec("SET CHARACTER SET 'utf8';");
$PDO -> exec("set names utf8");

我将此行设置为波斯语,您必须为您的语言设置第二行 utf8_persian_ci...
我对波斯语有同样的问题,这对我有帮助,希望对你也有帮助......

【讨论】:

  • @user3005741 我忘了写最后一行,这是代码的重要部分... $PDO-> exec("set names utf8");抱歉,现在试试这个...
猜你喜欢
  • 1970-01-01
  • 2015-02-26
  • 1970-01-01
  • 2011-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-30
相关资源
最近更新 更多