smclintock
smclintock

Reputation: 91

Zend Framework won't connect to MySQL database

I am starting a new project in work and using my preferred setup of PHP Zend Framework and a MySQL database.

For some reason I can't setup the Zend_Db_Adapter correctly so it will connect to the database successfully. I've setup my database in MySQL and can connect to it using mysql_connect(), but not with the code I've used from past projects? It's really strange!

Here is the code I'm using...

require_once 'Zend/Registry.php';
require_once 'Zend/Db/Adapter/Pdo/Mysql.php';

// Set the database connection information
$params = array('host' =>'localhost',
                'username'  =>'root',
                'password'  =>'12345',
                'dbname'    =>'testdb');

// Assign the database connection to the registry
$db = new Zend_Db_Adapter_Pdo_Mysql($params);
$db->setFetchMode(Zend_Db::FETCH_OBJ);
Zend_Registry::set('db',$db);

Please remember the above parameters work perfectly when I use mysql_connect(), but I get a controller exception error whenever I try using the above?

Upvotes: 1

Views: 2310

Answers (1)

Michael
Michael

Reputation: 1267

Try to use application.ini approach:

resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "root"
resources.db.params.password = "12345"
resources.db.params.dbname = "testdb"


Then, if you want to connect in bootstrap, you can do it manually (bootstrap will call it eventually itself):

$this->bootstrap('db');


And then access $db adapter:

$db = Zend_Db_Table::getDefaultAdapter();



Anyway if you prefer your approach, send us more data about exception.

Upvotes: 1

Related Questions