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 two rows (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: \ PhP5 \ Ext"

 


OK, let's go ..

 


Index. php homepage, which is also the only entry

 


<? PHP

//... Omitted

 


$ Params = array ('host' => '2017. 0.0.1 ',

'Username' => 'root ',

'Password' => '123 ',

'Dbname' => 'happycms ');

 


$ Db = zend_db: Factory ('pdomysql', $ Params );

Zend: Register ('db', $ dB );

?>

 


LIB/APP/Article. php

 


<? 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

 


}

 


?>

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

}

}

?>

AccessHttp: // 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.