Zend Framework Source Code Snippets

Configure Zend_Db to use an UTF-8 encoding

Bookmark and Share
1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
<?php
/**
 * Initialize database
 *
 * @return void */
public function initDb ()
{
    /* Configures PDO to execute the 'SET NAMES UTF8;' SQL query just before
       any other query. If no query is executed on your page, this will not be       executed. */
    $pdoParams = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8;');
    
    $params = array(
        'host' => 'localhost',        'username' => 'username',
        'password' => 'password',
        'dbname' => 'dbname',
        'driver_options' => $pdoParams
    ); 
    try {
        $db = Zend_Db::factory('PDO_MYSQL', $params);
        Zend_Db_Table_Abstract::setDefaultAdapter($db);
    } catch (Exception $e) {        exit($e->getMessage());
    }
 
    Zend_Registry::set('dbAdapter', $db);
}?>

Comments

Joseph Ch Joseph Ch
1 year ago

Zend_Db_Table_Abstract::setDefaultAdapter($db);

will be faster for couple milliseconds than

Zend_Db_Table::setDefaultAdapter($db);

because it doesn't need to "include" additional file and process "extends" expression.

Pierrick Dautrement Pierrick Dautrement
1 year ago

Thank you Joseph, I've changed the snippet.

David Caunt David Caunt
1 year ago

If your config is structure like

database.adapter = pdo_mysql
database.params.host = dbhost
database.params.username = dbuser
database.params.password = dbpass
database.params.dbname = dbname

but you still want to add pdo params, you can do

$config = new Zend_Config_Ini('config.ini', 'live');

$options = $config->database->params->toArray() + array('driver_options' => array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8;') );

$db = Zend_Db::factory($config->database->adapter, $options);

vikenbauer vikenbauer
1 year ago

Nice script, Pierrick.
If you want to connect using Mysqli adapter, things change slightly: you'll have to replace PDO::MYSQL_ATTR_INIT_COMMAND with MYSQLI_INIT_COMMAND (it's a mysqli php extension constant; at now there's no equivalent constant defined in Zend_Db nor Zend_Db_Adapter_Mysqli).

So line 12th has to be replaced for Mysqli as follows:
pdoParams = array(MYSQLI_INIT_COMMAND => 'SET NAMES UTF8;');
and 23th:
$db = Zend_Db::factory('MYSQLI', $params);

lemats lemats
1 year ago

add pdo parameters in your config file like:

database.params.driver_options.1002 = "SET NAMES utf8"

1002 is value of constant PDO::MYSQL_ATTR_INIT_COMMAND.
Don't know how to use constant.

David Caunt David Caunt
1 year ago

Ah yes...very good

gektor gektor
1 year ago

my.cnf/my.ini

[server]

init-connect="SET NAMES <encoding>"

Minh Son Nguyen Minh Son Nguyen
1 year ago

How I do that when I using Mysqli Adapter? I used $db->query('SET NAMES utf-8');, It works well but I think setting is better than that.

Nelius Nelius
1 year ago

resources.db.params.charset = "utf8"
for Zend_Aplication, or

db.params.charset = "utf8"
for any config

This will add "PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8'" automatically.

You can check it using:
print '<pre>';
print_r($db); //where $db is your db adapter

greg606 greg606
11 months ago

Nelius,

That's it!
resources.db.params.charset = "utf8" - works great!

But, do I really have to use it?
Isn't it possible to set it in the server configuration?

BTW, where can I find this parameter in zf documentation?

You must login before commenting on a snippet. If you do not have an account, please register.

Snippet description

The SQL command 'SET NAMES UTF8;' allows PHP to use UTF8 encoded strings from your database.

This snippet configures PDO to execute the 'SET NAMES UTF8;' SQL query just before any other query. If no query is executed on your page, this will not be executed.

Snippet details

Created:
Pierrick Dautrement Pierrick Dautrement
1 year ago
Edited:
Pierrick Dautrement Pierrick Dautrement
1 year ago
Revision Id:
12
Edit Message:
Initial Release
Tags:
zend_db encoding utf8
Comments:
10
Views:
1242
Points:
3 (3 votes)

History

r12

Initial Release

Pierrick Dautrement Pierrick Dautrement
1 year ago