ZendFramework中打开Pdo扩展连接MySql
时间:2009-06-24
来源:互联网
首先需要打开Pdo扩展。
在Windows目录下找到php.ini文件。打开extension=php_pdo_mysql.dll。
extension=php_pdo.dll
;extension=php_pdo_firebird.dll
;extension=php_pdo_mssql.dll
extension=php_pdo_mysql.dll
;extension=php_pdo_oci.dll
......
新建ZF工程如下图所示:
=700) window.open('http://images.phpfans.net/attach/images/20090624_99460820ca7db2f24eabsOfhser9t2Ay.jpg');" onload="if(this.offsetWidth>'700')this.width='700';if(this.offsetHeight>'700')this.height='700';" >
测试代码如下:
<?php
/**
*IndexController-Thedefaultcontrollerclass
*
*@author
*@version
*/
require_once'Zend/Controller/Action.php';
require_once'Zend/Db.php';
require_once'Zend/Registry.php';
require_once'Zend/Db/Table.php';
classIndexControllerextendsZend_Controller_Action
{
publicfunctioninit()
{
$params=array('host'=>'localhost',
'username'=>'root',
'password'=>'root',
'dbname' =>'mysql');
$db=Zend_Db::factory('Pdo_Mysql',$params);
Zend_Db_Table::setDefaultAdapter($db);
Zend_Registry::set('db',$db);
}
publicfunctionindexAction()
{
$adapter=Zend_Registry::get('db');
$result=$adapter->query('select*fromuser');
echo$result->rowCount();
echo$result->fetchAll();
}
}
这样就能和想要连接的数据库建立连接了。
关于如何取数据,请参看下面的代码:
<?php
/**
*IndexController-Thedefaultcontrollerclass
*
*@author
*@version
*/
require_once'Zend/Controller/Action.php';
require_once'Zend/Db.php';
require_once'Zend/Registry.php';
require_once'Zend/Db/Table.php';
classIndexControllerextendsZend_Controller_Action
{
publicfunctioninit()
{
$params=array('host'=>'localhost',
'username'=>'root',
'password'=>'root',
'dbname' =>'mysql');
$db=Zend_Db::factory('Pdo_Mysql',$params);
Zend_Db_Table::setDefaultAdapter($db);
Zend_Registry::set('db',$db);
}
publicfunctionindexAction()
{
$adapter=Zend_Registry::get('db');
$result=$adapter->query('select*fromuser');
echo$result->rowCount();
$rowset=$result->fetchAll();
foreach($rowsetas$row){
echo$row['Host'];
}
}
}
注意:fetchAll方法默认只能通过字段名称取数据,如果想通过数字(所在列的位置)取需要做如下处理:
......
publicfunctionindexAction()
{
$adapter=Zend_Registry::get('db');
$result=$adapter->query('select*fromuser');
echo$result->rowCount();
$rowset=$result->fetchAll(Zend_Db::FETCH_NUM);
foreach($rowsetas$row){
echo$row[0];
}
}
在Windows目录下找到php.ini文件。打开extension=php_pdo_mysql.dll。
extension=php_pdo.dll
;extension=php_pdo_firebird.dll
;extension=php_pdo_mssql.dll
extension=php_pdo_mysql.dll
;extension=php_pdo_oci.dll
......
新建ZF工程如下图所示:
=700) window.open('http://images.phpfans.net/attach/images/20090624_99460820ca7db2f24eabsOfhser9t2Ay.jpg');" onload="if(this.offsetWidth>'700')this.width='700';if(this.offsetHeight>'700')this.height='700';" >
测试代码如下:
<?php
/**
*IndexController-Thedefaultcontrollerclass
*
*@author
*@version
*/
require_once'Zend/Controller/Action.php';
require_once'Zend/Db.php';
require_once'Zend/Registry.php';
require_once'Zend/Db/Table.php';
classIndexControllerextendsZend_Controller_Action
{
publicfunctioninit()
{
$params=array('host'=>'localhost',
'username'=>'root',
'password'=>'root',
'dbname' =>'mysql');
$db=Zend_Db::factory('Pdo_Mysql',$params);
Zend_Db_Table::setDefaultAdapter($db);
Zend_Registry::set('db',$db);
}
publicfunctionindexAction()
{
$adapter=Zend_Registry::get('db');
$result=$adapter->query('select*fromuser');
echo$result->rowCount();
echo$result->fetchAll();
}
}
这样就能和想要连接的数据库建立连接了。
关于如何取数据,请参看下面的代码:
<?php
/**
*IndexController-Thedefaultcontrollerclass
*
*@author
*@version
*/
require_once'Zend/Controller/Action.php';
require_once'Zend/Db.php';
require_once'Zend/Registry.php';
require_once'Zend/Db/Table.php';
classIndexControllerextendsZend_Controller_Action
{
publicfunctioninit()
{
$params=array('host'=>'localhost',
'username'=>'root',
'password'=>'root',
'dbname' =>'mysql');
$db=Zend_Db::factory('Pdo_Mysql',$params);
Zend_Db_Table::setDefaultAdapter($db);
Zend_Registry::set('db',$db);
}
publicfunctionindexAction()
{
$adapter=Zend_Registry::get('db');
$result=$adapter->query('select*fromuser');
echo$result->rowCount();
$rowset=$result->fetchAll();
foreach($rowsetas$row){
echo$row['Host'];
}
}
}
注意:fetchAll方法默认只能通过字段名称取数据,如果想通过数字(所在列的位置)取需要做如下处理:
......
publicfunctionindexAction()
{
$adapter=Zend_Registry::get('db');
$result=$adapter->query('select*fromuser');
echo$result->rowCount();
$rowset=$result->fetchAll(Zend_Db::FETCH_NUM);
foreach($rowsetas$row){
echo$row[0];
}
}
作者: 淡水 发布时间: 2009-06-24
进来留名先~
=700) window.open('http://www.discuz.net/images/default/sigline.gif');" onload="if(this.offsetWidth>'700')this.width='700';if(this.offsetHeight>'700')this.height='700';" >
斗破苍穹 http://www.shucheng.com/book/5/5575/
=700) window.open('http://www.discuz.net/images/default/sigline.gif');" onload="if(this.offsetWidth>'700')this.width='700';if(this.offsetHeight>'700')this.height='700';" >
斗破苍穹 http://www.shucheng.com/book/5/5575/
作者: 霞奕严 发布时间: 2009-09-30
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28