Take the 2-minute tour ×

In one of our project the database connection specified in application.ini is

resources.db.adapter = Mysqli
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password = ''
resources.db.params.dbname = zf_tutorial

But I required to call the host, username, password and dbname from another file. how to do this?

share|improve this question
What do you mean exactly by "from another file" ? –  Songo Feb 13 '12 at 11:50
add comment

3 Answers

up vote 0 down vote accepted

The accepted answer here is not a good way to get the db object, it should be set up in application.ini as you were doing originally.

If you want to get another instance of the db object you have several options; you can do as @RockyFord suggests, or you can get the db object from any controller like this:-


If you want to go further and have access to the connection details (I can't imagine why you would need them again) then you can get them from the db object like this:-

$dbConfig = $db->getConfig();

or indeed:-

$dbConfig = $this->getInvokeArg('bootstrap')->getResource('db')->getConfig();

Which will give you an array containing the connection data, you can try this and do a var_dump($dbConfig); to see the details.

This is a much more robust way of accessing the db object if you need to, although I would take a long hard look at your overall code design if you need to do this in more than one place, especially if you are accessing the connection details as described here.

share|improve this answer
HI vascowhite, My requirement is that, we have a open source project coded in normal php4. we are now planing to update this project with zend framework. So presently we cannot upgrade this as whole. so plan to upgrade step by step. So initially we have to use the existing project db connection. But this db connection is dynamic. this project is further divided into sub domain so based on sub domain the database will change(user and password will remain same). So each sub domain the db change. –  Pradeep Feb 14 '12 at 5:34
add comment

usually with your database setup in the application.ini calling the default adapter is trivial
$db = Zend_Db_Table::getDefaultAdapter();
adding resources.db.isDefaultTableAdapter = true to the application.ini makes sure you're calling the correct adapter.
There are very situations where this may not work properly and most of them involve bootstrapping.

share|improve this answer
add comment

Usually, connection to the DB sets up in index.php:

// TODO: $adapter and $params from custom place, not hardcoded
$adapter = 'pdo_mysql';
$params = array(
    'host' => 'localhost',
    'username' => 'root',
    'password' => '',
    'dbname' => 'mydb'
$db = Zend_Db::factory($adapter, $params);

In this code $adapter and $params are hardcoded. You can get $adapter and $params from your custom file.

share|improve this answer
add comment

Your Answer


