下面的文章主要讲述的是zend_db 连接MySQL( 附完整代码),在看这些东西之前你确保是对PDO扩展以进行了正确的加载。我们的具体做法是编辑php.ini,对其进行手动增加下面这两行(前面要没有分号;): extension=php_pdo.dll extension=php_pdo_MySQL(和PHP搭
下面的文章主要讲述的是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所在目录,如
<ol class="dp-xml"><li class="alt"><span><span class="attribute">extension_dir</span><span> = </span><span class="attribute-value">"C:\php5\ext"</span><span> </span></span></li></ol>
index.php 网站首页,也是唯一入口
PHP代码如下:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"></span><span class="tag-name">php</span><span> </span></span></li> <li><span>//...省略 </span></li> <li class="alt"> <span>$</span><span class="attribute">params</span><span> = </span><span class="attribute-value">array</span><span> ('host' =</span><span class="tag">></span><span> '127.0.0.1', </span> </li> <li> <span>'username' =</span><span class="tag">></span><span> 'root', </span> </li> <li class="alt"> <span>'password' =</span><span class="tag">></span><span> '123456', </span> </li> <li> <span>'dbname' =</span><span class="tag">></span><span> 'happycms'); </span> </li> <li class="alt"> <span>$</span><span class="attribute">db</span><span> = </span><span class="attribute-value">Zend_Db</span><span>::factory('pdo</span>MySQL<span>(和PHP搭配之最佳组合)', $params); </span> </li> <li><span>Zend::register('db', $db); </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> <li><span>lib/App/Article.php </span></li> </ol>
zend_db 连接MySQL中:PHP代码如下:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"></span><span class="tag-name">php</span><span> </span></span></li> <li><span>class App_Article { </span></li> <li class="alt"><span>private $db; </span></li> <li><span>function App_Article() { </span></li> <li class="alt"> <span>$this-</span><span class="tag">></span><span class="attribute">db</span><span> = </span><span class="attribute-value">Zend</span><span>::registry('db'); </span> </li> <li><span>} </span></li> <li class="alt"><span>function listAll() { </span></li> <li> <span>$</span><span class="attribute">result</span><span> = $this-</span><span class="tag">></span><span>db-</span><span class="tag">></span><span>query('SELECT * FROM article'); </span> </li> <li class="alt"> <span>$</span><span class="attribute">rows</span><span> = $result-</span><span class="tag">></span><span>fetchAll(); </span> </li> <li><span>Zend::dump($rows); </span></li> <li class="alt"><span>} </span></li> <li><span>function listByCategory() { </span></li> <li class="alt"><span>} </span></li> <li><span>//...省略 </span></li> <li class="alt"><span>} </span></li> <li> <span class="tag">?></span><span> </span> </li> </ol>
PHP代码如下:
<ol class="dp-xml"> <li class="alt"><span><span>ArticleController.php </span></span></li> <li><span>class articleController extends Zend_Controller_Action { </span></li> <li class="alt"><span>private $view; </span></li> <li><span>private $article; </span></li> <li class="alt"><span>function __construct() { </span></li> <li> <span>$this-</span><span class="tag">></span><span class="attribute">view</span><span> = </span><span class="attribute-value">Zend</span><span>::registry('view'); </span> </li> <li class="alt"> <span>$this-</span><span class="tag">></span><span class="attribute">article</span><span> = </span><span class="attribute-value">new</span><span> App_Article(); </span> </li> <li><span>} </span></li> <li class="alt"><span>public function listAllAction() { </span></li> <li> <span>$this-</span><span class="tag">></span><span>article-</span><span class="tag">></span><span>listAll(); </span> </li> <li class="alt"> <span>$this-</span><span class="tag">></span><span>view-</span><span class="tag">></span><span class="attribute">title</span><span>=</span><span class="attribute-value">'View Articles'</span><span>; </span> </li> <li> <span>echo $this-</span><span class="tag">></span><span>view-</span><span class="tag">></span><span>render(TPL_DIR.'/tplView.php'); </span> </li> <li class="alt"><span>} </span></li> <li><span>function __call($action, $arguments) </span></li> <li class="alt"><span>{ </span></li> <li> <span>$this-</span><span class="tag">></span><span>_redirect('./'); </span> </li> <li class="alt"><span>print_r($action); </span></li> <li><span>print_r($arguments); </span></li> <li class="alt"><span>} </span></li> <li><span>} </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> </ol>
访问 http://happycms/article/listall
得到以下输出:
<ol class="dp-xml"> <li class="alt"><span><span>array(1) { </span></span></li> <li> <span>[0] =</span><span class="tag">></span><span> array(15) { </span> </li> <li class="alt"> <span>["articleid"] =</span><span class="tag">></span><span> string(1) "1" </span> </li> <li> <span>["categoryid"] =</span><span class="tag">></span><span> string(1) "0" </span> </li> <li class="alt"> <span>["articletitle"] =</span><span class="tag">></span><span> string(4) "test\" </span> </li> <li> <span>["articlefromwhere"] =</span><span class="tag">></span><span> string(3) "sdf" </span> </li> <li class="alt"> <span>["articlekeywords"] =</span><span class="tag">></span><span> string(5) "sdfds" </span> </li> <li> <span>["articledescription"] =</span><span class="tag">></span><span> string(4) "test" </span> </li> <li class="alt"> <span>["articlebody"] =</span><span class="tag">></span><span> string(9) "sffsdfsdf" </span> </li> <li> <span>["authorname"] =</span><span class="tag">></span><span> string(8) "haohappy" </span> </li> <li class="alt"> <span>["authoremail"] =</span><span class="tag">></span><span> string(11) "s...@df.com" </span> </li> <li> <span>["issticky"] =</span><span class="tag">></span><span> string(1) "0" </span> </li> <li class="alt"> <span>["isrecommanded"] =</span><span class="tag">></span><span> string(1) "0" </span> </li> <li> <span>["includeattachment"] =</span><span class="tag">></span><span> string(1) "0" </span> </li> <li class="alt"> <span>["addtime"] =</span><span class="tag">></span><span> string(19) "0000-00-00 00:00:00" </span> </li> <li> <span>["lastedittime"] =</span><span class="tag">></span><span> string(19) "0000-00-00 00:00:00" </span> </li> <li class="alt"> <span>["checktime"] =</span><span class="tag">></span><span> string(19) "0000-00-00 00:00:00" </span> </li> <li><span>} </span></li> </ol>
以上的相关内容就是对zend_db连接MySQL(附完整代码)的介绍,望你能有所收获。