zend_db 连接MySQL( 附完整代码)实操


  本文标签:连接MySQL

  下面的文章主要讲述的是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所在目录,如

  1. extension_dir = "C:\php5\ext" 

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

  PHP代码如下:

  

  1. php 
  2. //...省略  
  3. $params = array (host => 127.0.0.1,  
  4. username => root,  
  5. password => 123456,  
  6. dbname => happycms);  
  7. $db = Zend_Db::factory(pdoMySQL(和PHP搭配之最佳组合), $params);  
  8. Zend::register(db, $db);  
  9. ?>   
  10. lib/App/Article.php  

  

  zend_db 连接MySQL中:PHP代码如下:

  

  1. php 
  2. class App_Article {  
  3. private $db;  
  4. function App_Article() {  
  5. $this->db = Zend::registry(db);  
  6. }  
  7. function listAll() {  
  8. $result = $this->db->query(SELECT * FROM article);  
  9. $rows = $result->fetchAll();  
  10. Zend::dump($rows);  
  11. }  
  12. function listByCategory() {  
  13. }  
  14. //...省略  
  15. }  
  16. ?>   

  PHP代码如下:

  

  1. ArticleController.php  
  2. class articleController extends Zend_Controller_Action {  
  3. private $view;  
  4. private $article;  
  5. function __construct() {   
  6. $this->view = Zend::registry(view);  
  7. $this->article = new App_Article();   
  8. }   
  9. public function listAllAction() {  
  10. $this->article->listAll();  
  11. $this->view->title=View Articles;   
  12. echo $this->view->render(TPL_DIR./tplView.php);  
  13. }  
  14. function __call($action, $arguments)  
  15. {   
  16. $this->_redirect(./);  
  17. print_r($action);  
  18. print_r($arguments);  
  19. }  
  20. }  
  21. ?>   

  

  
访问 http://happycms/article/listall

  得到以下输出:

  

  1. array(1) {  
  2. [0] => array(15) {  
  3. ["articleid"] => string(1) "1"  
  4. ["categoryid"] => string(1) "0"  
  5. ["articletitle"] => string(4) "test\"  
  6. ["articlefromwhere"] => string(3) "sdf"  
  7. ["articlekeywords"] => string(5) "sdfds"  
  8. ["articledescription"] => string(4) "test"  
  9. ["articlebody"] => string(9) "sffsdfsdf"  
  10. ["authorname"] => string(8) "haohappy"  
  11. ["authoremail"] => string(11) "s...@df.com"  
  12. ["issticky"] => string(1) "0"  
  13. ["isrecommanded"] => string(1) "0"  
  14. ["includeattachment"] => string(1) "0"  
  15. ["addtime"] => string(19) "0000-00-00 00:00:00"  
  16. ["lastedittime"] => string(19) "0000-00-00 00:00:00"  
  17. ["checktime"] => string(19) "0000-00-00 00:00:00"  

  

  以上的相关内容就是对zend_db连接MySQL(附完整代码)的介绍,望你能有所收获  。