Mysql 使PDO连接utf8编码

Mysql 使PDO连接utf8编码,mysql,pdo,encoding,Mysql,Pdo,Encoding,我试图将我的连接编码为UTF8。我在看一些其他的帖子,但由于某些原因它不起作用。不知道我做错了什么 这就是我的conn的样子 $this->db_conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password); 这就是我想做的,但它说这是不正确的 $this->db_conn = new PDO("mys

我试图将我的连接编码为UTF8。我在看一些其他的帖子,但由于某些原因它不起作用。不知道我做错了什么

这就是我的conn的样子

 $this->db_conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
这就是我想做的,但它说这是不正确的

 $this->db_conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password.";charset=utf8");
给出此错误 数据库连接错误:的SQLSTATE[28000][1045]访问被拒绝


我正在使用MySql,它需要成为第一个参数的一部分。您的第二个版本应如下所示:

$this->db_conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name.";charset=utf8", $this->username, $this->password);

您遇到的错误是因为您的密码错误(您在密码后面附加了“;charset=utf8”,并且您的密码可能没有以该字符结尾)。

它需要作为第一个参数的一部分。您的第二个版本应如下所示:

$this->db_conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name.";charset=utf8", $this->username, $this->password);

您遇到的错误是因为您的密码错误(您在密码后面附加了“charset=utf8”,并且您的密码可能不会以该字符结尾)。

将您的字符集移到用户和密码之前

$this->db_conn = new PDO("mysql:host=" .
     $this->host .
     ";dbname="  . $this->db_name .
     ";charset=utf8" ,
     $this->username,
     $this->password);

将您的字符集移动到用户和密码之前

$this->db_conn = new PDO("mysql:host=" .
     $this->host .
     ";dbname="  . $this->db_name .
     ";charset=utf8" ,
     $this->username,
     $this->password);

哇,太简单了哇,太简单了