Connect zend_db to mysql (with complete code)

Source: Internet
Author: User

Before reading this, make sure that you have correctly loaded the PDO extension.

Edit php. ini
Manually Add the following two rows (there must be no semicolon before ;):
Extension = php_pdo.dll
Extension = 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: php5ext"

OK, lets go ..

Index. php homepage, which is also the only entry

 

The PHP code is as follows:

<? Php
//... Omitted

$ Params = array (host => 127.0.0.1,
Username => root,
Password = & gt; 123456,
Dbname => happycms );

$ Db = Zend_Db: factory (pdoMysql, $ params );
Zend: register (db, $ db );
?>


Lib/App/Article. php

 

The PHP code is as follows:

<? 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 (){
}

//... Omitted
}

?>

 


The PHP code is as follows:

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 );
}
}
?>


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.