【问题标题】:PDO Postgresql UTF8 charsetPDO Postgresql UTF8 字符集
【发布时间】:2016-11-21 17:01:33
【问题描述】:

我正在寻找一种使用 UTF8 字符集连接到 postgresql 数据库的方法。

目前,我必须在连接后发送查询请求以指定字符集。这根本不是最优的......

    $connect = new \PDO("pgsql:host=$this->host;dbname=$this->base", $this->user, $this->pass);
    $connect->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
    $connect->query("SET NAMES '$this->charset'");

MySQL 允许在参数上传递一个表来指定字符集,我正在寻找同样的东西。

        $this->db = new \PDO("pgsql:host=$this->PARAM_hote;dbname=$this->PARAM_nom_bd", $this->PARAM_utilisateur, $this->PARAM_mot_passe, array(\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''));
        $this->db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

【问题讨论】:

标签: php sql postgresql pdo character-encoding


【解决方案1】:

去:

new PDO('pgsql:host=' .  $db_host . ';dbname=' . $db_name . ';options=\'--client_encoding=UTF8\'', $db_username, $db_password, array(PDO::ATTR_PERSISTENT => true));

事实上,传递选项--client_encoding=UTF8的单引号非常重要,双引号不起作用。

【讨论】:

    【解决方案2】:

    您可以像这样传递“选项”键:

    $dbh = new PDO
      (
        "pgsql:dbname=mydbname;host=myhost;port=5432;options='--client_encoding=UTF8'",
        "username",
        "password"
       );
    

    【讨论】:

      【解决方案3】:

      尝试在构造函数中添加参数:

      $connect = new \PDO("pgsql:host=$this->host;dbname=" . $this->base . ";options='-c client_encoding=utf8'", $this->user, $this->pass);
      $connect->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
      

      【讨论】:

        猜你喜欢
        • 2010-11-28
        • 2018-01-15
        • 1970-01-01
        • 2012-04-15
        • 2013-05-23
        • 2017-09-29
        • 1970-01-01
        • 1970-01-01
        • 2016-07-23
        相关资源
        最近更新 更多