Search code examples
phpzend-frameworkzend-db

how to access mysql link connection resource from zend mysql DB connection object


Here is how i am creating the zend db connection

$DB = new Zend_Db_Adapter_Pdo_Mysql(array("host" => "localhost","username" => "root", "password" => "admin123", "dbname" => "user_management"));

The problem i have is that in my model files mysql_query have been used to run queries. I want to pass the DB connection from the controller to the model. How do i get the php factory mysql link resource directly from the zend db connection object.

PS: I have tried adding

$db = $DB->getConnection();

This isnt working, I think it maybe because of some include file missing.


Solution

  • I like to open the connection by application.ini

    resources.db.adapter = "PDO_MYSQL"
    resources.db.params.host = "localhost"
    resources.db.params.username = "root"
    resources.db.params.password = ""
    resources.db.params.dbname = "dbtest"
    resources.db.isDefaultTableAdapter = true
    

    If you're using Zend Framework 1.8+ (and if you use resources.db in application.ini), you can get an instance of db Adapter through Zend_Db_Table everywhere:

    $dbAdapter = Zend_Db_Table::getDefaultAdapter();