问题
I am trying to connect to multiple databases with Zend Framework. When I connect using the same credentials with mysql_connect, it works just fine. When I connect with Zend I get
"Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[28000] [1045] Access
denied for user 'xxxx'@'localhost' (using password: YES)' in C:\wamp\www\zend\library\Zend\Db\Adapter\Pdo\Abstract.php on line 144"
I was following the tutorial at http://www.amazium.com/blog/using-different-databases-with-zend-framework
Here is my application.ini:
resources.multidb.db1.adapter = "PDO_MYSQL"
resources.multidb.db1.dbname = "xxxxx"
resources.multidb.db1.username = "xxxxxx"
resources.multidb.db1.password = "xxxxxx"
resources.multidb.db1.hostname = "localhost"
resources.multidb.db1.charset = "UTF8"
resources.multidb.db1.default = true
resources.multidb.db3.adapter = "PDO_MYSQL"
resources.multidb.db3.dbname = "xxxxxx"
resources.multidb.db3.username = "xxxxxx"
resources.multidb.db3.password = "xxxxxx"
resources.multidb.db3.hostname = "xxxxx"
resources.multidb.db3.charset = "UTF8"
resources.multidb.db3.default = false
Here is my Bootstrap.php:
/**
* Add databases to the registry
*/
public function _initDbRegistry()
{
$this->bootstrap('multidb');
$multidb = $this->getPluginResource('multidb');
Zend_Registry::set('local', $multidb->getDb('db1'));
Zend_Registry::set('db2', $multidb->getDb('db2'));
Zend_Registry::set('db3', $multidb->getDb('db3'));
}
Here is my model:
class ModuleName_Model_ModelName extends Zend_Db_Table_Abstract
{
protected $_name;
protected $_schema;
protected $_db; // changed from $_adapter in tutorial for Zend 1.11
public function init()
{
$this->_name = 'contacts';
$this->_schema = 'xxxxx';
$this->_db = Zend_Registry::get('db3');
}
}
Here is where I'm calling it in my controller:
$model = new ModuleName_Model_ModelName();
$this->view->output= $model->fetchAll();
I have connectected via phpmyadmin, mysql workbench, and using plain old mysql_connect by copying and pasting the same username and password. There is no doubt that the username and password are correct. Anyone have any idea what I could be doing wrong in Zend that is throwing the access denied error?
Ok, thanks - I tried your suggestion, Midhun:
Zend_Debug::dump($model->getAdapter()->getConfig());
and I did get correct output.
Any other ideas?
回答1:
try out this for a debug and check if the adapter is exactly what you expect it to be...
$model = new ModuleName_Model_ModelName();
Zend_Debug::dump($model->getAdapter()->getConfig());
来源:https://stackoverflow.com/questions/5200492/cant-connect-to-external-database-with-zend-framework-but-mysql-connect-works