How to install UTF-8 in PDO class constructor for PHP PgSQL database

I want to set UTF8 for my PDO object. This class works correctly with MySQL. I can not find the analogue array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF8") for PgSQL, and I can not work with Cyrillic characters.

 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") ); } 
+6
source share
3 answers

From what I see in section 21.2.3 on this page , you can use one of the following two commands:

  • SET CLIENT_ENCODING TO 'value';
  • SET NAMES 'value';

where value = UTF8 . Try using:

 SET CLIENT_ENCODING TO 'UTF8'; 

or

 SET NAMES 'UTF8'; 
+3
source

Let me point out a comment by xmedeko, i.e. absolute right:

 pg_connect("host=localhost options='--client_encoding=UTF8'"); 

Source: http://php.net/manual/en/function.pg-connect.php

Using charset = utf8 works (only) with mysql ...

+4
source

it’s very easy to find an analog for a regular SQL query

 $pdo->query("SET NAMES UTF8") 

However, the encoding must be set to DSN anyway

 $this->conn = new PDO("pgsql:host=".$this->host.";dbname=".$this->db.";charset=".$this->charset 
+1
source

Source: https://habr.com/ru/post/951794/


All Articles