php SQLSTATE[HY000] [2019]未知字符集错误

daupos2t  于 2023-05-27  发布在  PHP
关注(0)|答案(1)|浏览(266)

我目前的代码是这样的,我相信它会导致上面的错误消息出现。我试图在我的网站上创建一个预订页面,任何帮助将不胜感激。

<?php
class Reservation {
  // (A) PROPERTIES
  private $pdo; // PDO object
  private $stmt; // SQL statement
  public $error; // Error message

  // (B) CONSTRUCTOR - CONNECT TO DATABASE
  function __construct() {
    try {
      $this->pdo = new PDO(
        "mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset= utf8".DB_CHARSET,
        DB_USER,DB_PASSWORD,[PDO::ATTR_ERRMODE =>PDO::ERRMODE_EXCEPTION]
      );
    } catch (Exception $ex) { die($ex->getMessage()); }
  }

  // (C) DESTRUCTOR - CLOSE DATABASE CONNECTION
  function __destruct() {
    $this->pdo = null;
    $this->stmt = null;
  }
5kgi1eie

5kgi1eie1#

我也犯了同样的错误。我试图从cakephp连接到mariadb。问题是我使用了mariadb服务器上不存在的编码。
cakephp配置:

'encoding' => 'utf8mb3'

mariadb编码列表:

MariaDB [(none)]> show character set like 'utf%';
+---------+------------------+--------------------+--------+
| Charset | Description      | Default collation  | Maxlen |
+---------+------------------+--------------------+--------+
| utf8    | UTF-8 Unicode    | utf8_general_ci    |      3 |
| utf8mb4 | UTF-8 Unicode    | utf8mb4_general_ci |      4 |
| utf16   | UTF-16 Unicode   | utf16_general_ci   |      4 |
| utf16le | UTF-16LE Unicode | utf16le_general_ci |      4 |
| utf32   | UTF-32 Unicode   | utf32_general_ci   |      4 |
+---------+------------------+--------------------+--------+

当我将cakephp配置更改为:

'encoding' => 'utf8'

这个问题解决了。

相关问题