I have the following code in application.ini

resources.db.adapter=PDO_MYSQL
resources.db.params.host=localhost
resources.db.params.username=myusername
resources.db.params.password=mypassword
resources.db.params.dbname=mydb
resources.db.params.charset = "utf8"
resources.db.params.driver_options.1002 = "SET NAMES utf8"

I cheked on the internet and could not find out an answer, may you could help.

Why the last row

resources.db.params.driver_options.1002 = "SET NAMES utf8"

is used, if the charset has already been set in the previous line:

resources.db.params.charset = "utf8"

?

Recommended Answers

All 3 Replies

The reason is probably given by their PDO library, check lines 266, 320 and 323:

The $options, created by the resources.db.params.driver_options.1002 variable is used as 4th argument in the PDO connection string, for the character set. The $charset variable instead is used at line 323:

$this->resource->exec('SET NAMES ' . $this->resource->quote($charset));

So, it seems they are repeating the command for the pgsql driver. Bye.

Sorry for the update, I just noticed that you are using ZF1, the situation is similar, but the code is different and it refers to MySQL, check lines 83 and 104:

/**
 * Creates a PDO object and connects to the database.
 *
 * @return void
 * @throws Zend_Db_Adapter_Exception
 */
protected function _connect()
{
    if ($this->_connection) {
        return;
    }

    if (!empty($this->_config['charset'])
        && version_compare(PHP_VERSION, '5.3.6', '<')
    ) {
        $initCommand = "SET NAMES '" . $this->_config['charset'] . "'";
        $this->_config['driver_options'][1002] = $initCommand; // 1002 = PDO::MYSQL_ATTR_INIT_COMMAND
    }

    parent::_connect();
}

When they get PHP versions lower than 5.3.6, it seems, they rewrite the driver_options with the value of charset. It seems like that value in the application.ini is a default value, it probably could be empty.

From the PHP docs:

charset
The character set. See the character set concepts documentation for more information.
Prior to PHP 5.3.6, this element was silently ignored. The same behaviour can be partly replicated with the PDO::MYSQL_ATTR_INIT_COMMAND driver option, as the following example shows.

@cereal
Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.