下面的文章主要講述的是zend_db 連接MySQL( 附完整代碼),在看這些東西之前你確保是對PDO擴展以進行了正確的加載。我們的具體做法是編輯php.ini,對其進行手動增加下面這兩行前面要沒有分號;):
extension=php_pdo.dll
extension=php_pdo_MySQL(和PHP搭配之最佳組合).dll
然後要把extension_dir
指向php_pdo.dll及php_pdo_MySQL(和PHP搭配之最佳組合).dll所在目錄,如
- extension_dir = "C:\php5\ext"
index.php 網站首頁,也是唯一入口
PHP代碼如下:
- <?php
- //...省略
- $params = array ('host' => '127.0.0.1',
- 'username' => 'root',
- 'password' => '123456',
- 'dbname' => 'happycms');
- $db = Zend_Db::factory('pdoMySQL(和PHP搭配之最佳組合)', $params);
- Zend::register('db', $db);
- ?>
- lib/App/Article.php
zend_db 連接MySQL中:PHP代碼如下:
- <?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() {
- }
- //...省略
- }
- ?>
PHP代碼如下:
- ArticleController.php
- class articleController extends Zend_Controller_Action {
- private $view;
- private $article;
- function __construct() {
- $this->view = Zend::registry('view');
- $this->article = new App_Article();
- }
- public function listAllAction() {
- $this->article->listAll();
- $this->view->title='View Articles';
- echo $this->view->render(TPL_DIR.'/tplView.php');
- }
- function __call($action, $arguments)
- {
- $this->_redirect('./');
- print_r($action);
- print_r($arguments);
- }
- }
- ?>
訪問 http://happycms/article/listall
得到以下輸出:
- array(1) {
- [0] => array(15) {
- ["articleid"] => string(1) "1"
- ["categoryid"] => string(1) "0"
- ["articletitle"] => string(4) "test\"
- ["articlefromwhere"] => string(3) "sdf"
- ["articlekeywords"] => string(5) "sdfds"
- ["articledescription"] => string(4) "test"
- ["articlebody"] => string(9) "sffsdfsdf"
- ["authorname"] => string(8) "haohappy"
- ["authoremail"] => string(11) "[email protected]"
- ["issticky"] => string(1) "0"
- ["isrecommanded"] => string(1) "0"
- ["includeattachment"] => string(1) "0"
- ["addtime"] => string(19) "0000-00-00 00:00:00"
- ["lastedittime"] => string(19) "0000-00-00 00:00:00"
- ["checktime"] => string(19) "0000-00-00 00:00:00"
- }
以上的相關內容就是對zend_db連接MySQL(附完整代碼)的介紹,望你能有所收獲。