1

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?

5
  • What version of ZF? Look @ Zend_Db_Factory. Commented Sep 17, 2012 at 22:04
  • it's version 1.12.0 - pretty sure i've tried Zend_Db_Factory but will try again. thanks Commented Sep 17, 2012 at 22:45
  • If not solved, what is the controller exception error? Can you trap and display? Commented Sep 18, 2012 at 0:27
  • Could you put a die; at the end of each line to find out which line is throwing the exception? Commented Sep 18, 2012 at 9:52
  • You should specify your connection parameters in application.ini. Commented Sep 19, 2012 at 7:58

1 Answer 1

1

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.

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

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.