溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

zend framework如何配置操作數(shù)據(jù)庫

發(fā)布時間:2021-10-18 09:39:08 來源:億速云 閱讀:104 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要為大家展示了“zend framework如何配置操作數(shù)據(jù)庫”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“zend framework如何配置操作數(shù)據(jù)庫”這篇文章吧。

zendframework項目環(huán)境搭建后,看了下zend framework配置操作數(shù)據(jù)庫,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數(shù)據(jù)庫來測試下,就用wp_posts表來測試吧:
首先模型models建立Wp_posts.php

復制代碼 代碼如下:


<?php
class Wp_posts extends Zend_Db_Table{
protected $_name = 'Wp_posts';
protected $_primary = 'ID';
}
?>


控制器controller下面建立IndexController.php

復制代碼 代碼如下:


<?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

復制代碼 代碼如下:


<html>
<head>
<title>this is for test</title>
</head>
<body>
<table>
<?php foreach ($this->res as $news){?>
<tr>
<td><?php echo $news['id']?></td>
<td><?php echo $news['post_title']?></td>
<td><?php echo $news['post_date']?> </td>
</tr>
<?php }?>
</table>
</body>
</html>

以上是“zend framework如何配置操作數(shù)據(jù)庫”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI