无法使用Zend Framework连接到外部数据库,但是mysql_connect可以工作 [英] Can't connect to external database with Zend Framework but mysql_connect works

查看:78
本文介绍了无法使用Zend Framework连接到外部数据库,但是mysql_connect可以工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Zend Framework连接到多个数据库.当我使用相同的凭据与mysql_connect连接时,它可以正常工作.当我与Zend建立联系时,我会得到

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"

我在 http://www上关注本教程. amazium.com/blog/using-different-databases-with-zend-framework

这是我的application.ini:

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

这是我的Bootstrap.php:

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'));
}

这是我的模特:

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();

我已经通过phpmyadmin,mysql工作台并通过复制和粘贴相同的用户名和密码使用普通的旧mysql_connect进行了连接.毫无疑问,用户名和密码是正确的.有人知道在Zend中我可能会做错什么,引发访问被拒绝错误吗?

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?

好,谢谢-我尝试了你的建议,Midhun:

Ok, thanks - I tried your suggestion, Midhun:

Zend_Debug::dump($model->getAdapter()->getConfig());

我确实得到了正确的输出.

and I did get correct output.

还有其他想法吗?

推荐答案

尝试进行调试,并检查适配器是否正是您所期望的...

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());

这篇关于无法使用Zend Framework连接到外部数据库,但是mysql_connect可以工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆