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啦,瀏覽器顯示:
http://www.bkjia.com/PHPjc/326241.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/326241.htmlTechArticlezendframework項目環境搭建後,看了下zend framework配置操作資料庫,php教程如下: 在application/configs的檔案下建立一個config.ini檔案 配置資訊如下...