TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
Database PDO wrong charset
  • Hello!

    I have an iteresting problem. I set correctly the database config file, and plain db connection works perfectly, but when i want to use PDO the charset is wrong. I tried both server (Ubuntu 13.04 LAMP server) and localhost (W8, fresh XAMPP) with the same result. What is the problem?

    Here is my database config file:

    <?php defined('SYSPATH') OR die('No direct access allowed.');
    
    return array
    (
        'default' => array
        (
            'type'       => 'MySQL',
            'connection' => array(
                /**
                 * The following options are available for MySQL:
                 *
                 * string   hostname     server hostname, or socket
                 * string   database     database name
                 * string   username     database username
                 * string   password     database password
                 * boolean  persistent   use persistent connections?
                 * array    variables    system variables as "key => value" pairs
                 *
                 * Ports and sockets may be appended to the hostname.
                 */
                'hostname'   => 'localhost',
                'database'   => 'kohana',
                'username'   => 'root',
                'password'   => '',
                'persistent' => FALSE,
            ),
            'table_prefix' => '',
            'charset'      => 'utf8',
            'caching'      => FALSE,
        ),
        'alternate' => array(
            'type'       => 'PDO',
            'connection' => array(
                /**
                 * The following options are available for PDO:
                 *
                 * string   dsn         Data Source Name
                 * string   username    database username
                 * string   password    database password
                 * boolean  persistent  use persistent connections?
                 */
                'dsn'        => 'mysql:host=localhost;dbname=kohana',
                'username'   => 'root',
                'password'   => '',
                'persistent' => FALSE,
            ),
            /**
             * The following extra options are available for PDO:
             *
             * string   identifier  set the escaping identifier
             */
            'table_prefix' => '',
            'charset'      => 'utf8',
            'caching'      => FALSE,
        ),
    );
    

    Thanks!

    mobal,

  • Try add 'options' => array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'), after 'persistent' => FALSE, in PDO 'connection' section.

  • @pumpkin thanks for your fix. It is now working fine! Something like Kohana bug or what is this issue?

    Thanks!

    mobal,

  • With PDO the recommended way of setting the charset for MySQL is setting it in the DSN. See http://php.net/manual/en/ref.pdo-mysql.connection.php

  • @cs278 thanks. Now it is "clear"! :)

  • Thanks. Had same issue.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion