简体   繁体   English

使用Zend_Db_Adapter_PDO_Mysql类连接到mysql数据库

[英]connect to mysql db using Zend_Db_Adapter_PDO_Mysql class

How to connect to mysql db correctly? 如何正确连接到mysql数据库? In my application.ini file, I have put all the db details: 在我的application.ini文件中,我放置了所有的数据库详细信息:

[master]
adapter = PDO_MYSQL
params.host = localhost
params.username = root
params.password = ''
params.dbname = accounts_db

I want to get these details and connect to my db. 我想获取这些详细信息并连接到我的数据库。

Try this one its work 试试这个工作

resources.db.adapter = PDO_MYSQL
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password = 
resources.db.params.dbname = accounts_db

In my site it will work perfectly 在我的网站上,它将完美运行

When it's the default mysql database adapter you can add the line at the bottom, in that case it will be auto. 当它是默认的mysql数据库适配器时,您可以在底部添加该行,在这种情况下它将是自动的。 used in Zend_Db_Table objects etc. 在Zend_Db_Table对象等中使用

resources.db.adapter="pdo_mysql"
resources.db.params.host="localhost"
resources.db.params.username="username"
resources.db.params.password="pass"
resources.db.params.dbname="dbname"                       
resources.db.isDefaultTableAdapter=true   

On the otherside you can fetch the adapter at any place with 另一方面,您可以在任何地方使用

Zend_Db_Table::getDefaultAdapter();

Good luck. 祝好运。

Zend Framework is not the easiest framework to start application development. Zend Framework不是开始应用程序开发的最简单的框架。 My best advice is to start reading the quickstart documentation , especially chapters dealing with Zend_Application , Zend_Db , Zend_Layout and of course Zend_Controller . 我最好的建议是开始阅读快速入门文档 ,尤其是涉及Zend_ApplicationZend_DbZend_Layout以及当然Zend_Controller的章节。

To answer your question, adding database configuration to application.ini isn't setting up the db connection. 要回答您的问题,将数据库配置添加到application.ini并不设置数据库连接。 You should bootstrap an application resource using: 您应该使用以下方法引导应用程序资源:

[production]

; Database (production)
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "my_user_who_is_not_root"
resources.db.params.password = "my_strong_password"
resources.db.params.dbname = "accounts_db"
resources.db.isDefaultTableAdapter = true

; Other configurations
; ...

[development : production]

; Database (development settings)
resources.db.params.username = "root"
resources.db.params.password = ""
resources.db.params.dbname = "accounts_db_dev"

At this point, your database connection is ready to query the server . 至此, 您的数据库连接已准备就绪,可以查询服务器了

Into your controllers (or anything else), you can retrieve the connection using: 在控制器(或其他任何东西)中,您可以使用以下方法检索连接:

Zend_Db_Table::getDefaultAdapter();

You could also within your Bootstrap add a reference to the connection to the Zend_Register : 您还可以在Bootstrap添加对Zend_Register连接的引用:

protected function _initDbRegister()
{
    $db = $this->bootstrap('Db')->getResource('Db');
    Zend_Register::set('Zend_Db', $db);
}

Later, you could call Zend_Register::get('Zend_Db') to get the instance. 稍后,您可以调用Zend_Register::get('Zend_Db')来获取实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM