开发者

Problem executing SQL query in Zend?

开发者 https://www.devze.com 2023-03-09 17:42 出处:网络
I was executing sql query in zend something like this and it was working: $front = Zend_Controller_Front::getInstance();

I was executing sql query in zend something like this and it was working:

$front = Zend_Controller_Front::getInstance();
$bootstrap = $front->getParam('bootstrap');
$resource = $bootstrap->getPluginResource('db');
$dbAdapter = $resource->getDbAdapter();
$statement = $dbAdapter->query("SELECT * from tablename");
$results = $statement->fetchAll();

At that time my application.ini was something like this:

resources.db.adapter = "Mysqli"
resources.db.params.host = "localhost"
resources.db.params.username = "username"
reso开发者_StackOverflow社区urces.db.params.password = "password"
resources.db.params.dbname = "dbname"

Question:

Now I changed my application.ini to:

resources.multidb.local.adapter = "Mysqli"
resources.multidb.local.host = "localhost"
resources.multidb.local.username = "username"
resources.multidb.local.password = "passwod"
resources.multidb.local.dbname = "dbname"
resources.multidb.local.default = true

Now above zend code is producing following error:

Fatal error: Call to a member function getDbAdapter() on a non-object in Mapper.php  on line 297

How to fix my code according to new application.ini settings ?

Thanks


To retrieve the default Database Adapter use the following code:

$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
$resource = $bootstrap->getPluginResource('multidb');
$db = $resource->getDb();


Following code is working for me with multidb configuration in application.ini

$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
$resource = $bootstrap->getPluginResource('multidb');
$db = $resource->getDb();
$statement = $db->query("SELECT * from tablename");
$results = $statement->fetchAll();
0

精彩评论

暂无评论...
验证码 换一张
取 消