How to connect zend_db to MySQL (with complete code)

Source: Internet
Author: User
The following article describes how to connect zend_db to MySQL (with complete code). Before reading these things, make sure that the PDO extension is loaded correctly. Our specific practice is to edit php. ini and manually Add the following two lines (there is no semicolon before;): extensionphp_pdo.dllextensionphp_pdo_MySQL (set up with PHP

The following article describes how to connect zend_db to MySQL (with complete code). Before reading these things, make sure that the PDO extension is loaded correctly. Our specific practice is to edit php. ini and manually Add the following two lines (there is no semicolon before;): extension = php_pdo.dll extension = php_pdo_MySQL (with PHP

The following article describes how to connect zend_db to MySQL (with complete code). Before reading these things, make sure that the PDO extension is loaded correctly. You can edit php. ini and manually Add the following two rows (there is no semicolon before ;):

Extension = php_pdo.dll

Extension = php_pdo_MySQL (the best combination with PHP). dll

Then, set extension_dir

Point to the directory of php_pdo.dll and php_pdo_MySQL (the best combination with PHP). dll, as shown in figure

 
 
  1. extension_dir = "C:\php5\ext"

Index. php homepage, which is also the only entry

The PHP code is as follows:

 
 
  1. //... Omitted
  2. $ Params = array ('host' => '2017. 0.0.1 ',
  3. 'Username' => 'root ',
  4. 'Password' => '123 ',
  5. 'Dbname' => 'happycms ');
  6. $ Db = Zend_Db: factory ('pdomysql (best combination with PHP) ', $ params );
  7. Zend: register ('db', $ db );
  8. ?>
  9. Lib/App/Article. php

Zend_db is connected to MySQL: the PHP code is as follows:

 
 
  1. Class App_Article {
  2. Private $ db;
  3. Function App_Article (){
  4. $ This-> db = Zend: registry ('db ');
  5. }
  6. Function listAll (){
  7. $ Result = $ this-> db-> query ('select * FROM article ');
  8. $ Rows = $ result-> fetchAll ();
  9. Zend: dump ($ rows );
  10. }
  11. Function listByCategory (){
  12. }
  13. //... Omitted
  14. }
  15. ?>

The PHP code is as follows:

 
 
  1. ArticleController.php
  2. class articleController extends Zend_Controller_Action {
  3. private $view;
  4. private $article;
  5. function __construct() {
  6. $this->view = Zend::registry('view');
  7. $this->article = new App_Article();
  8. }
  9. public function listAllAction() {
  10. $this->article->listAll();
  11. $this->view->title='View Articles';
  12. echo $this->view->render(TPL_DIR.'/tplView.php');
  13. }
  14. function __call($action, $arguments)
  15. {
  16. $this->_redirect('./');
  17. print_r($action);
  18. print_r($arguments);
  19. }
  20. }
  21. ?>


Access http: // happycms/article/listall

The following output is displayed:

 
 
  1. array(1) {
  2. [0] => array(15) {
  3. ["articleid"] => string(1) "1"
  4. ["categoryid"] => string(1) "0"
  5. ["articletitle"] => string(4) "test\"
  6. ["articlefromwhere"] => string(3) "sdf"
  7. ["articlekeywords"] => string(5) "sdfds"
  8. ["articledescription"] => string(4) "test"
  9. ["articlebody"] => string(9) "sffsdfsdf"
  10. ["authorname"] => string(8) "haohappy"
  11. ["authoremail"] => string(11) "s...@df.com"
  12. ["issticky"] => string(1) "0"
  13. ["isrecommanded"] => string(1) "0"
  14. ["includeattachment"] => string(1) "0"
  15. ["addtime"] => string(19) "0000-00-00 00:00:00"
  16. ["lastedittime"] => string(19) "0000-00-00 00:00:00"
  17. ["checktime"] => string(19) "0000-00-00 00:00:00"
  18. }

The above content is an introduction to connecting zend_db to MySQL (with the complete code). I hope you will get some benefits.

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.