使用Zend Registry连接到数据库
我是新来的Zend框架。我试图使用Zend Registry连接数据库,但我无法做到。使用Zend Registry连接到数据库
这是我的引导类。
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
public function _initDb(){
$db = new Zend_Db_Adapter_Pdo_Mysql(
array(
'host' => 'localhost',
'username' => 'ragn',
'password' => 'app',
'dbname' => 'apple'
)
);
Zend_Db_Table_Abstract::setDefaultAdapter($db);
Zend_Registry::set('db', $db);
}
}
?>
这里是我的IndexController
类。
class IndexController extends Zend_Controller_Action {
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
$db = Zend_Registry::get('dbadapter');
$select = new Zend_Db_Select($db);
$select = $db->select();
$select = $db->select() -> from(
array('client' => 'client'),
array ('idclient')) -> join(array('apartmentclient' => 'apartmentclient'),
'client.idclient = apartmentclient.idclient',
array ('idapartment')) -> join(array('description' => 'description'),
'apartmentclient.idapartment = description.idapartment',array ('title'));
$stmt = $select->query();
$result = $stmt->fetchAll();
$Settext = array();
$SetName = array();
$SetName = "$result[2]";
$Settext = "Hi!Can you please provide feedback for the apartment";
$this->view->Settext = $Settext." ".$SetName;
}
}
我还有详细的application.ini文件。
resources.db.adapter = PDO_MYSQL
resources.db.isDefaultAdapter = true
resources.db.params.host = localhost
resources.db.params.username = ragn
resources.db.params.password = app
resources.db.params.dbname = apple
任何人都可以指向正确的方向吗?
在你的代码中有错误。您在注册表中设置数据库中使用的关键“DB”:
Zend_Registry::set('db', $db);
,但您尝试使用该密钥“将对DBAdapter”找回它:
$db = Zend_Registry::get('dbadapter');
它应该是这样的:
$db = Zend_Registry::get('db');
而且这条线是无用:
$select = $db->select();
这条线:
$SetName = "$result[2]";
可以是这样的:
$SetName = $result[2];
既然你写这行:
$result = $stmt->fetchAll();
你有一个循环来获取值。
要发现错误,您必须使用调试器,但如果您不知道如何使用它,最简单的方法是使用函数die();
。沿着你的代码移动这个函数,当你发现脚本没有停下来,并且返回你写的错误时,你发现有问题的行。
嗨,很多很多谢谢你的回复。现在我已经在application.ini中删除了我的代码并在indexcontroller.But中更改了$ db = Zend_Registry :: get('db');但仍然收到类似致命错误的错误:最大执行时间超过30秒C:\ xampp \ htdocs \ Firstzen \ library \ Zend \ Db \ Statement \ Pdo.php在线221.由于我是PHP的新手,我真的无法知道它是什么。请告诉我什么是错误的? – user1230541 2012-07-16 10:21:24
@ user1230541 Answer updated – 2012-07-16 13:13:36
首先,您可以从您的引导程序中删除方法_initDb()。由于ZF为您完成此通过阅读你的application.ini
第二你并不需要你的数据库存放在注册表自从存储在注册表的目的是从你喜欢的地方,可以简单地通过
来实现访问的东西$db = Zend_Db_Table::getDefaultAdapter();
你是什么意思“无法”?数据库是否限制访问?你有适当的权限连接到数据库?该数据库是否与* exact *用户和登录凭据一起存在?你能在这里填空吗? – Makoto 2012-07-15 23:34:05
Hi.Thanks的答复。我只是试图查询它。它说最大执行时间30秒超过。是正在尝试使用正确的登录凭据。我不知道我需要在其他文件中的条目像.htaccess?..... – user1230541 2012-07-15 23:40:15
你真的只需要在application.ini或bootstrap.php中设置你的数据库适配器。您正在设置相同的适配器两次。'$ db = new Zend_Config($ this-> getOption('db');'在你的引导程序中将从application.ini获得适配器 – RockyFord 2012-07-16 04:27:10