zend_db连接mysql(附完整代码)

2007-06-08 10:35:42  作者

在看这些之前请确保你正确加载了PDO扩展。

作法是编辑php.ini
手动增加下面这两行(前面要没有分号;)
extension=php_pdo.dll
extension=php_pdo_mysql.dll

然后要把extension_dir
指向php_pdo.dll及php_pdo_mysql.dll所在目录,如
extension_dir = "C:\php5\ext"

OK,let's go..

index.php 网站首页,也是唯一入口

PHP代码如下:

%26lt;?php
//...省略

$params = array ('host' =%26gt; '127.0.0.1',
'username' =%26gt; 'root',
'password' =%26gt; '123456',
'dbname' =%26gt; 'happycms');

$db = Zend_Db::factory('pdoMysql', $params);
Zend::register('db', $db);
?%26gt;


lib/App/Article.php

PHP代码如下:

%26lt;?php
class App_Article {
private $db;
function App_Article() {
$this-%26gt;db = Zend::registry('db');
}

function listAll() {
$result = $this-%26gt;db-%26gt;query('SELECT * FROM article');
$rows = $result-%26gt;fetchAll();

Zend::dump($rows);
}

function listByCategory() {
}

//...省略
}

?%26gt;


PHP代码如下:

ArticleController.php
class articleController extends Zend_Controller_Action {
private $view;
private $article;

function __construct() {
$this-%26gt;view = Zend::registry('view');
$this-%26gt;article = new App_Article();
}

public function listAllAction() {
$this-%26gt;article-%26gt;listAll();
$this-%26gt;view-%26gt;title='View Articles';
echo $this-%26gt;view-%26gt;render(TPL_DIR.'/tplView.php');
}

function __call($action, $arguments)
{
$this-%26gt;_redirect('./');
print_r($action);
print_r($arguments);
}
}
?%26gt;


访问 http://happycms/article/listall

得到以下输出

array(1) {
[0] =%26gt; array(15) {
["articleid"] =%26gt; string(1) "1"
["categoryid"] =%26gt; string(1) "0"
["articletitle"] =%26gt; string(4) "test\"
["articlefromwhere"] =%26gt; string(3) "sdf"
["articlekeywords"] =%26gt; string(5) "sdfds"
["articledescription"] =%26gt; string(4) "test"
["articlebody"] =%26gt; string(9) "sffsdfsdf"
["authorname"] =%26gt; string(8) "haohappy"
["authoremail"] =%26gt; string(11) "s...@df.com"
["issticky"] =%26gt; string(1) "0"
["isrecommanded"] =%26gt; string(1) "0"
["includeattachment"] =%26gt; string(1) "0"
["addtime"] =%26gt; string(19) "0000-00-00 00:00:00"
["lastedittime"] =%26gt; string(19) "0000-00-00 00:00:00"
["checktime"] =%26gt; string(19) "0000-00-00 00:00:00"
}