温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

zend framework如何配置操作数据库

发布时间:2021-10-18 09:39:08 来源:亿速云 阅读:95 作者:小新 栏目:开发技术

这篇文章主要为大家展示了“zend framework如何配置操作数据库”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“zend framework如何配置操作数据库”这篇文章吧。

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

复制代码 代码如下:


<?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如何配置操作数据库”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI