postgresql 如何在 PHP PgSQL 数据库的 PDO 类构造函数中设置 UTF-8
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18250167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How set UTF-8 in PDO class constructor for PHP PgSQL database
提问by vili
I want to set UTF8 for my PDO object. This class works correctly with MySQL.
I can't find an analog of array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF8")
for PgSQL and I can't work with cyrillic symbols.
我想为我的 PDO 对象设置 UTF8。此类与 MySQL 一起正常工作。我找不到array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF8")
PgSQL的类似物,也无法使用西里尔符号。
class oop{
private $host="localhost";
private $user="xxxx";
private $db="xxxx";
private $pass="111111";
private $conn;
public function __construct(){
$this->conn = new PDO("pgsql:host=".$this->host.";dbname=".$this->db,$this->user,$this->pass,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF8") );
}
采纳答案by hjpotter92
From what I see in section 21.2.3 on this page, you can use one of the following two commands:
根据我在本页第 21.2.3 节中看到的,您可以使用以下两个命令之一:
SET CLIENT_ENCODING TO 'value';
SET NAMES 'value';
SET CLIENT_ENCODING TO 'value';
SET NAMES 'value';
where value = UTF8
. Try using:
其中值 = UTF8
。尝试使用:
SET CLIENT_ENCODING TO 'UTF8';
or
或者
SET NAMES 'UTF8';
回答by redaxmedia
Let me point out the comment by xmedeko, that is absolute right:
让我指出xmedeko的评论,这是绝对正确的:
pg_connect("host=localhost options='--client_encoding=UTF8'");
Source: http://php.net/manual/en/function.pg-connect.php
来源:http: //php.net/manual/en/function.pg-connect.php
Using charset=utf8 is working (only) with mysql...
使用 charset=utf8 可以(仅)与 mysql 一起使用...
回答by Your Common Sense
it is very easy to find an analog for the regular SQL query
很容易找到常规 SQL 查询的类似物
$pdo->query("SET NAMES UTF8")
However, encoding have to be set in DSN anyway
但是,无论如何都必须在 DSN 中设置编码
$this->conn = new PDO("pgsql:host=".$this->host.";dbname=".$this->db.";charset=".$this->charset