PHP PDO: charset, set names?

前端 未结 9 722
孤独总比滥情好
孤独总比滥情好 2020-11-22 06:49

I had this previously in my normal mysql_* connection:

mysql_set_charset(\"utf8\",$link);
mysql_query(\"SET NAMES \'UTF8\'\");

Do I need it

9条回答
  •  终归单人心
    2020-11-22 07:19

    I think you need an additionally query because the charset option in the DSN is actually ignored. see link posted in the comment of the other answer.

    Looking at how Drupal 7 is doing it in http://api.drupal.org/api/drupal/includes--database--mysql--database.inc/function/DatabaseConnection_mysql%3A%3A__construct/7:

    // Force MySQL to use the UTF-8 character set. Also set the collation, if a
    // certain one has been set; otherwise, MySQL defaults to 'utf8_general_ci'
    // for UTF-8.
    if (!empty($connection_options['collation'])) {
      $this->exec('SET NAMES utf8 COLLATE ' . $connection_options['collation']);
    }
    else {
      $this->exec('SET NAMES utf8');
    }
    

提交回复
热议问题