Actual Operation Scheme for zend_db to correctly connect to the MySQL database

Source: Internet
Author: User

The following articles mainly describe the correct solution for zend_db to correctly connect to the MySQL database in actual operations. If you encounter similar situations in actual operations, however, you do not know how to solve the problem correctly, so the following articles must be good teachers and friends for you.

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

Edit php. ini

Manually Add the following two rows without a semicolon ;)

 
 
  1. Extension = php_pdo.dll
  2. 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" 
  2. OK,let's go..  

Index. php homepage, which is also the only entry

The PHP code is as follows:

<? Php

Omitted

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

The PHP code is as follows:

 
 
  1. <?php 
  2. class App_Article {  
  3. private $db;  
  4. function App_Article() {  
  5. $this->db = Zend::registry('db');  
  6. }  
  7. function listAll() {  
  8. $result = $this->db->query('SELECT * FROM article');  
  9. $rows = $result->fetchAll();  
  10. Zend::dump($rows);  
  11. }  
  12. function listByCategory() {  
  13. }  

Omitted

 
 
  1. }  
  2. ?>   

In actual operations on connecting zend_db to MySQL (with complete code), we need to use the PHP Code 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"  

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.