1

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?

1
  • What do you mean exactly by "from another file" ? Commented Feb 13, 2012 at 11:50

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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);
Zend_Db_Table_Abstract::setDefaultAdapter($db);

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

Comments

0

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:-

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

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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.