zend framework配置操作数据库实例分析_PHP教程
程序员文章站
2022-05-05 21:10:56
...
zendframework项目环境搭建后,看了下zend framework配置操作数据库,php教程如下:
在application/configs的文件下建立一个config.ini文件
配置信息如下:
[general]
db.adapter=PDO_MYSQL
db.config.host=localhost/IParess
db.config.username=username
db.config.password=password
db.config.dbname=databasename
2、
在pulibc 目录的index.php页面中
/** Zend_Application */
require_once 'Zend/Application.php';
的下面插入
//set the datase config
require_once 'Zend/Config/Ini.php';
require_once 'Zend/Registry.php';
require_once 'Zend/Db.php';
require_once 'Zend/Db/Table.php';
$config=new Zend_Config_Ini('./../application/configs/config.ini',null, true);
Zend_Registry::set('config',$config);
$dbAdapter=Zend_Db::factory($config->general->db->adapter,$config->general->db->config->toArray());
$dbAdapter->query('SET NAMES UTF8');
Zend_Db_Table::setDefaultAdapter($dbAdapter);
Zend_Registry::set('dbAdapter',$dbAdapter);
就此,我就用我的本地wordpress数据库来测试下,就用wp_posts表来测试吧:
首先模型models建立Wp_posts.php
class Wp_posts extends Zend_Db_Table{
protected $_name = 'Wp_posts';
protected $_primary = 'ID';
}
?>
控制器controller下面建立IndexController.php
require_once APPLICATION_PATH.'/models/Wp_posts.php';
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
$con = new Wp_posts();
$res = $con->fetchAll()->toArray();
$this->view->res = $res;
$this->render("index");
}
}
在views/scripts/index/ 建立视图:index.phtml
this is for test
ok啦,浏览器显示:
在application/configs的文件下建立一个config.ini文件
配置信息如下:
[general]
db.adapter=PDO_MYSQL
db.config.host=localhost/IParess
db.config.username=username
db.config.password=password
db.config.dbname=databasename
2、
在pulibc 目录的index.php页面中
/** Zend_Application */
require_once 'Zend/Application.php';
的下面插入
//set the datase config
require_once 'Zend/Config/Ini.php';
require_once 'Zend/Registry.php';
require_once 'Zend/Db.php';
require_once 'Zend/Db/Table.php';
$config=new Zend_Config_Ini('./../application/configs/config.ini',null, true);
Zend_Registry::set('config',$config);
$dbAdapter=Zend_Db::factory($config->general->db->adapter,$config->general->db->config->toArray());
$dbAdapter->query('SET NAMES UTF8');
Zend_Db_Table::setDefaultAdapter($dbAdapter);
Zend_Registry::set('dbAdapter',$dbAdapter);
就此,我就用我的本地wordpress数据库来测试下,就用wp_posts表来测试吧:
首先模型models建立Wp_posts.php
复制代码 代码如下:
class Wp_posts extends Zend_Db_Table{
protected $_name = 'Wp_posts';
protected $_primary = 'ID';
}
?>
控制器controller下面建立IndexController.php
复制代码 代码如下:
require_once APPLICATION_PATH.'/models/Wp_posts.php';
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
$con = new Wp_posts();
$res = $con->fetchAll()->toArray();
$this->view->res = $res;
$this->render("index");
}
}
在views/scripts/index/ 建立视图:index.phtml
复制代码 代码如下:
ok啦,浏览器显示:
上一篇: 这是什么有关问题呢
下一篇: PHP操作数据库有关问题
推荐阅读
-
zend framework配置操作数据库实例分析
-
Windows上php5.6操作mongodb数据库示例【配置、连接、获取实例】
-
Zend Framework框架教程之Zend_Db_Table_Rowset用法实例分析_php实例
-
Zend Framework教程之连接数据库并执行增删查的方法(附demo源码下载)_php实例
-
Zend Framework入门之环境配置及第一个Hello World示例(附demo源码下载)_php实例
-
Zend Framework教程之配置文件application.ini解析_php实例
-
Zend Framework教程之动作的基类Zend_Controller_Action详解_php实例
-
Zend Framework教程之Application用法实例详解_php实例
-
PHP操作文本数据库实例教程
-
Zend Framework教程之资源(Resources)用法实例详解,zendframework_PHP教程