ZendFramework中打开Pdo扩展连接MySql

本文介绍了如何使用Zend Framework进行数据库连接配置,并演示了通过PDO_Mysql驱动查询数据的方法。此外,还提供了如何获取查询结果及按列位置取数据的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先需要打开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.     }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值