Home >php教程 >PHP开发 >Analysis of Zend Framework connecting to Mysql database instance

Analysis of Zend Framework connecting to Mysql database instance

高洛峰
高洛峰Original
2016-12-26 15:56:181210browse

The example in this article describes the method of Zend Framework connecting to Mysql database. Share it with everyone for your reference, the details are as follows:

Please make sure you have loaded the PDO extension correctly before reading these. The way to do this is to edit php.ini.
Add these two lines manually (without semicolon;):

extension=php_pdo.dll
extension=php_pdo_mysql.dll

Then point extension_dir

to php_pdo.dll And the directory where php_pdo_mysql.dll is located, such as

extension_dir = "C:/php5/ext"

OK,let's go..

index.php The homepage of the website is also the only entrance

<?php
//...省略
$params = array (&#39;host&#39;   => &#39;127.0.0.1&#39;,
         &#39;username&#39; => &#39;root&#39;,
         &#39;password&#39; => &#39;123456&#39;,
         &#39;dbname&#39;  => &#39;happycms&#39;);
$db = Zend_Db::factory(&#39;pdoMysql&#39;, $params);
Zend::register(&#39;db&#39;, $db);
?>

lib/App/Article.php

<?php
class App_Article {
    private $db;
    function App_Article() {
        $this->db = Zend::registry(&#39;db&#39;);
    }
    function listAll() {
        $result = $this->db->query(&#39;SELECT * FROM article&#39;);
        $rows = $result->fetchAll();
        Zend::dump($rows);
    }
    function listByCategory() {
    }
    //...省略
}
?>

ArticleController.php

class articleController extends Zend_Controller_Action {
  private $view;
  private $article;
  function __c****truct() {
    $this->view = Zend::registry(&#39;view&#39;);
    $this->article = new App_Article();
  }
  public function listAllAction() {
    $this->article->listAll();
    $this->view->title=&#39;View Articles&#39;;
    echo $this->view->render(TPL_DIR.&#39;/tplView.php&#39;);
  }
  function __call($action, $arguments)
  {
    $this->_redirect(&#39;./&#39;);
    print_r($action);
    print_r($arguments);
  }
}
?>

Visit http://happycms/article/listall

and get the following output:

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"
 }

I hope this article will be helpful to everyone’s PHP programming based on the Zend Framework framework.

For more articles related to Zend Framework connection to Mysql database instance analysis, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn