This article describes how to connect ZendFramework to the Mysql database and analyzes the steps and implementation skills of ZendFramework to connect to the MySQL database in the form of a complete instance, for more information about how to connect Zend Framework to a Mysql database, see the following example. We will share this with you for your reference. The details are as follows:
Before reading this, make sure that you have correctly loaded the PDO extension. Edit php. ini.
Manually add the two rows (no semicolon before ;):
extension=php_pdo.dllextension=php_pdo_mysql.dll
Then, set extension_dir
Point to the directory where php_pdo.dll and php_pdo_mysql.dll are located, as shown in figure
extension_dir = "C:/php5/ext"
OK, let's go ..
Index. php homepage, which is also the only entry
<? Php //... omit $ params = array ('host' => '2017. 0.0.1 ', 'username' => 'root', 'Password' => '000000', 'dbname' => 'happycms'); $ db = Zend_Db :: factory ('pdomysql', $ params); Zend: register ('db', $ db);?>
Lib/App/Article. php
<? Phpclass 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 (){}//... omitted}?>
ArticleController. php
class articleController extends Zend_Controller_Action { private $view; private $article; function __c****truct() { $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); }}?>
Access http: // happycms/article/listall
The following output is displayed:
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) "s...@df.com" ["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" }