ZendFramework中打开Pdo扩展连接MySql

来源:互联网 发布:卡佩拉数据 编辑:程序博客网 时间:2024/05/06 03:14

首先需要打开Pdo扩展。

在Windows目录下找到php.ini文件。打开extension=php_pdo_mysql.dll。

  1. extension=php_pdo.dll
  2. ;extension=php_pdo_firebird.dll
  3. ;extension=php_pdo_mssql.dll
  4. extension=php_pdo_mysql.dll
  5. ;extension=php_pdo_oci.dll
  6. ......

新建ZF工程如下图所示:

测试代码如下:

  1. <?php
  2. /**
  3.  * IndexController - The default controller class
  4.  * 
  5.  * @author
  6.  * @version 
  7.  */
  8. require_once 'Zend/Controller/Action.php';
  9. require_once 'Zend/Db.php';
  10. require_once 'Zend/Registry.php';
  11. require_once 'Zend/Db/Table.php';
  12. class IndexController extends Zend_Controller_Action 
  13. {
  14.     public function init()
  15.     {
  16.         $params = array ('host' => 'localhost',
  17.                          'username' => 'root',
  18.                          'password' => 'root',
  19.                          'dbname'   => 'mysql');
  20.         $db = Zend_Db::factory('Pdo_Mysql'$params);
  21.         Zend_Db_Table::setDefaultAdapter($db);
  22.         Zend_Registry::set('db'$db);
  23.     }
  24.     
  25.     public function indexAction() 
  26.     {
  27.         $adapter = Zend_Registry::get('db');
  28.         $result = $adapter->query('select * from user');
  29.         echo $result->rowCount();
  30.         echo $result->fetchAll();
  31.     }
  32. }

这样就能和想要连接的数据库建立连接了。

关于如何取数据,请参看下面的代码:

  1. <?php
  2. /**
  3.  * IndexController - The default controller class
  4.  * 
  5.  * @author
  6.  * @version 
  7.  */
  8. require_once 'Zend/Controller/Action.php';
  9. require_once 'Zend/Db.php';
  10. require_once 'Zend/Registry.php';
  11. require_once 'Zend/Db/Table.php';
  12. class IndexController extends Zend_Controller_Action 
  13. {
  14.     public function init()
  15.     {
  16.         $params = array ('host' => 'localhost',
  17.                          'username' => 'root',
  18.                          'password' => 'root',
  19.                          'dbname'   => 'mysql');
  20.         $db = Zend_Db::factory('Pdo_Mysql'$params);
  21.         Zend_Db_Table::setDefaultAdapter($db);
  22.         Zend_Registry::set('db'$db);
  23.     }
  24.     
  25.     public function indexAction() 
  26.     {
  27.         $adapter = Zend_Registry::get('db');
  28.         $result = $adapter->query('select * from user');
  29.         echo $result->rowCount();
  30.         $rowset = $result->fetchAll();
  31.         foreach ($rowset as $row) {
  32.             echo $row['Host'];
  33.         }
  34.     }
  35. }

注意:fetchAll方法默认只能通过字段名称取数据,如果想通过数字(所在列的位置)取需要做如下处理:

  1.     ......
  2.     public function indexAction() 
  3.     {
  4.         $adapter = Zend_Registry::get('db');
  5.         $result = $adapter->query('select * from user');
  6.         echo $result->rowCount();
  7.         $rowset = $result->fetchAll(Zend_Db::FETCH_NUM);
  8.         foreach ($rowset as $row) {
  9.             echo $row[0];
  10.         }
  11.     }
原创粉丝点击