0

How to connect to mysql db correctly? In my application.ini file, I have put all the db details:

[master]
adapter = PDO_MYSQL
params.host = localhost
params.username = root
params.password = ''
params.dbname = accounts_db

I want to get these details and connect to my db.

1
  • I doubt its urgent, and this is just lazy. Commented Jul 11, 2012 at 6:02

3 Answers 3

1

Try this one its work

resources.db.adapter = PDO_MYSQL
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password = 
resources.db.params.dbname = accounts_db

In my site it will work perfectly

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

1 Comment

still you are not able to connect then let me know
1

When it's the default mysql database adapter you can add the line at the bottom, in that case it will be auto. used in Zend_Db_Table objects etc.

resources.db.adapter="pdo_mysql"
resources.db.params.host="localhost"
resources.db.params.username="username"
resources.db.params.password="pass"
resources.db.params.dbname="dbname"                       
resources.db.isDefaultTableAdapter=true   

On the otherside you can fetch the adapter at any place with

Zend_Db_Table::getDefaultAdapter();

Good luck.

Comments

1

Zend Framework is not the easiest framework to start application development. My best advice is to start reading the quickstart documentation, especially chapters dealing with Zend_Application, Zend_Db, Zend_Layout and of course Zend_Controller.

To answer your question, adding database configuration to application.ini isn't setting up the db connection. You should bootstrap an application resource using:

[production]

; Database (production)
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "my_user_who_is_not_root"
resources.db.params.password = "my_strong_password"
resources.db.params.dbname = "accounts_db"
resources.db.isDefaultTableAdapter = true

; Other configurations
; ...

[development : production]

; Database (development settings)
resources.db.params.username = "root"
resources.db.params.password = ""
resources.db.params.dbname = "accounts_db_dev"

At this point, your database connection is ready to query the server.

Into your controllers (or anything else), you can retrieve the connection using:

Zend_Db_Table::getDefaultAdapter();

You could also within your Bootstrap add a reference to the connection to the Zend_Register:

protected function _initDbRegister()
{
    $db = $this->bootstrap('Db')->getResource('Db');
    Zend_Register::set('Zend_Db', $db);
}

Later, you could call Zend_Register::get('Zend_Db') to get the instance.

Comments

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.