2015-09-28 8 views
5

Questa è una parte della mia classe DOP. Devo usare utf-8 per la lingua ebraica ma quando ho impostato ATTR_PERSISTENT su true il testo di output verrà visualizzato come ?????? Se si passa da ATTR_PERSISTENT a false, l'output sarà corretto.PHP PDO, Impossibile impostare il nome mentre la connessione è persistente?

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(); 
    } 
} 

Esiste un conflitto tra:

PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8' 

E:

PDO::ATTR_PERSISTENT => true 
+0

Quali sono i tipi di file di codifica? – rray

+0

@rray È utf-8 –

+0

Ciò che è curioso, è che è stato [chiesto prima] (http://stackoverflow.com/questions/29714253/pdoattr-persistent-utf-8-encoding) e non ha catturato quasi nessuna attenzione. –

risposta

2

sono riuscito a trovare la risposta here.

impostazione nel DSN è l'unico modo corretto, così ho cambiato il codice a questo:

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(); 
     } 
    } 

E ora l'uscita è giusto.

+0

Felice di vederlo aiutato :) –