本文實例講述了Zend Framework連接Mysql資料庫的方法。分享給大家供大家參考,具體如下:
在看這些之前請確保你正確加載了PDO擴充。做法是編輯php.ini 。
手動增加這兩行(前面要沒有分號;):
extension=php_pdo.dll extension=php_pdo_mysql.dll
然後把extension_dir
指向php_pdo.dllllo_pdo_mysql.dllmdo_mysql.d5 ,let's go..
index.php 網站首頁,也是唯一入口
extension_dir = "C:/php5/ext"
lib/App/Article.php
<?php //...省略 $params = array ('host' => '127.0.0.1', 'username' => 'root', 'password' => '123456', 'dbname' => 'happycms'); $db = Zend_Db::factory('pdoMysql', $params); Zend::register('db', $db); ?>
.
訪問http:// happycms/article/listall得到以下輸出:<?php class App_Article { private $db; function App_Article() { $this->db = Zend::registry('db'); } function listAll() { $result = $this->db->query('SELECT * FROM article'); $rows = $result->fetchAll(); Zend::dump($rows); } function listByCategory() { } //...省略 } ?>