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 ;)
- 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
- extension_dir = "C:\php5\ext"
- OK,let's go..
Index. php homepage, which is also the only entry
The PHP code is as follows:
<? Php
Omitted
- $ Params = array ('host' => '2017. 0.0.1 ',
- 'Username' => 'root ',
- 'Password' => '123 ',
- 'Dbname' => 'happycms ');
- $ Db = Zend_Db: factory ('pdomysql (best combination with PHP) ', $ 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
- }
- ?>
In actual operations on connecting zend_db to MySQL (with complete code), we need to use the PHP Code 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"
- }
The above content is an introduction to connecting zend_db to MySQL (with the complete code). I hope you will get some benefits.