PHP PDO, Can't set name while connection remains constant?

This is part of my PDO class. I need to use utf-8 for Hebrew, but when I set ATTR_PERSISTENT to true , the output text will be shown as ?????? If I switch ATTR_PERSISTENT to false , the output will be right.

 public function __construct() { // Set DSN $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname; // Set options $options = array( PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_PERSISTENT => true ); // Create a new PDO instanace try { $this->dbh = new PDO($dsn, $this->user, $this->pass, $options); } // Catch any errors catch (PDOException $e) { $this->error = $e->getMessage(); } } 

Is there a conflict between:

 PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8' 

and

 PDO::ATTR_PERSISTENT => true 
+5
source share
1 answer

I could find the answer here .

Installing it in a DSN is the only correct way, so I changed the code to this:

  public function __construct() { // Set DSN $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname .';charset=utf8'; // Set options $options = array( //PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8", PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_PERSISTENT => true ); // Create a new PDO instanace try { $this->dbh = new PDO($dsn, $this->user, $this->pass, $options); } // Catch any errors catch (PDOException $e) { $this->error = $e->getMessage(); } } 

And now the result is right.

+2
source

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


All Articles