配置文件

 <db>
<adapter>PDO_MSSQL</adapter>
<config>
<host>localhost</host>
<port></port>
<username>sa</username>
<password></password>
<dbname>edudb</dbname>
<pdoType>sqlsrv</pdoType>
</config>
</db> <!-- 测试多数据库 -->
<db2>
<adapter>PDO_MSSQL</adapter>
<config>
<host>localhost</host>
<port></port>
<username>sa</username>
<password></password>
<dbname>test</dbname>
<pdoType>sqlsrv</pdoType>
</config>
</db2>

入口文件

//配置数据库连接
$db_config = $web_config->db->config->toArray();
//var_dump($db_config);
$db = Zend_Db::factory($web_config->db->adapter, $db_config);
//var_dump($db);
//exit;
//$db->query('set NAMES utf8');
//$db->getProfiler()->setEnabled(false);
Zend_Db_Table::setDefaultAdapter($db);

这里是默认的数据库

dao.php调用默认数据库

 $db = &$this->getAdapter();

dao2.php连接其他数据库

function init() {
$web_config = $this->getCfg();
$this->db2_config = $web_config->db2->config->toArray();
//var_dump($this->db_config);
$this->db = Zend_Db::factory($web_config->db2->adapter, $this->db2_config);
Zend_Db_Table::setDefaultAdapter($this->db);
} public function returnDb(){
return $this->db;
}

调用

 $db = &$this->getAdapter();
还是会连接默认数据库。

直接使用

$this->db就可以了

来看一下完整的dao2.php
<?php

class dao_dao2 extends Zend_Db_Table {
protected $cfg_; function init() {
$web_config = $this->getCfg();
$this->db2_config = $web_config->db2->config->toArray();
//var_dump($this->db_config);
$this->db = Zend_Db::factory($web_config->db2->adapter, $this->db2_config);
Zend_Db_Table::setDefaultAdapter($this->db);
} public function returnDb(){
return $this->db;
} public function getData($table,$where = false, $order = 'id ASC', $pagesize = false, $offset = false, $count = false, $from = false, $join = false, $group = false) {
//$this->db = &$this->getAdapter();
$select = $this->db->select(); if ($where && is_array($where)) {
foreach ($where as $key => $val) {
//print_r($where);
if($val['type']==){
$select->where($key, $this->convert2gbk($val['val']));
}else{
$select->orwhere($key, $this->convert2gbk($val['val']));
} }
} if (!$from)
$from = '*';
//echo $select."<br/>";
if ($pagesize) {
$select->limit($pagesize, $offset);
}
//echo $select."<br/>";
if (is_array($order)) {
foreach ($order as $value) {
$select->order($value);
}
} else {
$select->order($order);
}
//echo $select."<br/>";
$select->from($table, $count ? "COUNT(".$table.".id)" : $from); if (is_array($group)) {
foreach ($group as $key => $val) {
$select->group($val);
} if ($count) {
$result = $this->db->fetchAll($select);
//echo $select."<br/>";
return $result;
}
} else {
if ($count) {
$result = $this->db->fetchOne($select);
//echo $select."<br/>";
return $result;
}
}
if (is_array($join)) {
foreach ($join as $key => $val) {
//$select->join($key, $val[0], $val[1]);
$select->joinleft($key, $val[], $val[]);
}
} //echo $select."<br/>"; //echo $select;exit; $result = $this->db->fetchAll($select);
foreach ($result as $key => $value) {
foreach ($value as $key2 => $value2) {
$result[$key][$key2] = $this->convert2utf8($value2);
}
}
return $result;
} /**
* 向表中插入数据
* array $adata 数据
* string $table 表名
* int $insterid 是否需要返回插入ID
* @return true or false or int
*/
// @bianding 2013.11.04 更改了pdo中mssql.php的lastInsertId()函数
// @bianding 2013.11.04 经测试 mssql.php中的lastInsertId()函数中的SELECT两种方式都行
function SaveData($adata, $table, $insterid = , $aLog = false) {
//$this->db = &$this->getAdapter();
foreach ($adata as $key => $value) {
$adata[$key] = $this->convert2gbk($value);
}
if ($this->db->insert($table, $adata)) {
//var_dump($this->db->getProfiler());
$insertedID = $this->db->lastInsertId();
if ($insterid) {
return $insertedID;
} else {
return TRUE;
}
} else {
return false;
}
} /**
* 删除表中数据
*
* @param string $table 表名
* @param string $where 'id ='.$id 条件
* @return true or false
*/
function DelData($table, $where, $aLog = false) {
//$this->db = & $this->getAdapter();
if ($this->db->delete($table, $where)) {
return TRUE;
} else {
return FALSE;
}
} /**
* 更新表中数据
*
* @param string $table
* @param array $adata
* @param string $where 'id ='.$id
* @return true or false
*/
function UpdateData($table, $adata, $cond, $aLog = false) {
//$this->db = & $this->getAdapter();
foreach ($adata as $key => $value) {
$adata[$key] = $this->convert2gbk($value);
}
if ($this->db->update($table, $adata, $cond)) {
return TRUE;
} else {
return false;
}
} public function clearTable($table) {
//$this->db = &$this->getAdapter();
$result = $this->db->query('TRUNCATE TABLE ' . $table);
} public function executeSql($strSql) {
//$this->db = &$this->getAdapter();
$result = $this->db->query($strSql);
} function convert2utf8($string)
{
$config = $this->getCfg();
$pdoType = $config->db->config->pdoType;
if($pdoType == 'dblib'){
return iconv("gbk","utf-8",$string);
}elseif($pdoType == 'sqlsrv'){
//$encode = mb_detect_encoding($string, array('UTF-8',"GB2312",'GBK','BIG5'));
//echo $encode;
return mb_convert_encoding($string,"UTF-8","UTF-8");
//return $string;
}
}
function convert2gbk($string)
{
$config = $this->getCfg();
$pdoType = $config->db->config->pdoType;
if($pdoType == 'dblib'){
return iconv("utf-8","gbk",$string);
}elseif($pdoType == 'sqlsrv'){
//$encode = mb_detect_encoding($string, array('UTF-8',"GB2312",'GBK','BIG5'));
//echo $encode;
return mb_convert_encoding($string,"UTF-8","UTF-8");
//return $string;
}
} protected function &getCfg() {
if ($this->cfg_ === null) {
$registry = Zend_Registry::getInstance();
$this->cfg_ = $registry->get('web_config');
}
return $this->cfg_;
} }
												

Zendframework连接两个或多个数据库的实现的更多相关文章

  1. Thinkphp框架下连接两个及以上的数据库方法

    在我们的实际开发者,我们经常需要链接两个以上的数据库,方法跟简单 Thinkphp文档中也有介绍:点击查看 方法如下: 第一步:配置文件config.php <?php //默认数据库1 ret ...

  2. Linq to Entity中连接两个数据库时要注意的问题

    Linq to Entity中连接两个数据库时要注意的问题 今天大学同学问了我一个问题,Linq to Entity中连接两个数据库时,报错“指定的 LINQ 表达式包含对与不同上下文关联的查询的引用 ...

  3. Android 通过外键连接两个数据库

    Learn: 1.Android数据库的语法. 2.通过外键连接两个数据库. 3.加强了对数据库的熟悉度. 4.对文本框的visiblity属性的了解. Demo:http://pan.baidu.c ...

  4. Springboot配置连接两个数据库

    背景: 项目中需要从两个不同的数据库查询数据,之前实现方法是:springboot配置连接一个数据源,另一个使用jdbc代码连接. 为了改进,现在使用SpringBoot配置连接两个数据源 实现效果: ...

  5. [转]oracle10客户端PL/SQL Developer如何连接远程服务器上的oracle数据库

    时间:2013年8月21日 前提条件:假设你已经安装好了oracle和PL/SQL Developer,知道远程服务器的IP和数据库端口,知道远程服务器上的oracle数据库名和密码 如何用PL/SQ ...

  6. sqlserver2014两台不同服务器上数据库同步

    sqlserver2014两台不同服务器上数据库同步   同步了快一个月了,哈哈,因为途中比较麻烦,第一次,遇到烦的地方就停下了,今天终于同步成功了,哈哈,下面我就来介绍一下我实现两台数据库同步的过程 ...

  7. [转]sqlserver2014两台不同服务器上数据库同步

    https://www.cnblogs.com/peng0731/p/7359465.html 同步了快一个月了,因为途中比较麻烦,第一次,遇到烦的地方就停下了,今天终于同步成功了,哈哈,下面我就来介 ...

  8. windows下通过navicat for mysql连接centos6.3-64bit下的MySQL数据库

    一.centos下MySQL安装 按照命令依次安装以下文件: mysql-devel 开发用到的库以及包含文件 mysql mysql 客户端 mysql-server 数据库服务器 yum inst ...

  9. JavaScript concat() 方法-连接两个或多个数组

    一,定义和用法 concat() 方法用于连接两个或多个数组. 该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本. 语法 arrayObject.concat(arrayX,arrayX,. ...

随机推荐

  1. Android导航菜单横向左右滑动并和下方的控件实现联动

    这个是美团网个人订单的效果,找了很多地方都没找到,自己研究了两天终于弄出来了^_^,有什么问题希望大家指出来,谢谢. 实现原理是上方使用HorizontalScrollView这个可以水平横向拖动的控 ...

  2. MySQL流程控制函数

    官方文档:Control Flow Functions Name Description CASE Case operator IF() If/else construct IFNULL() Null ...

  3. Check iO:初学Python

    The end of other For language training our Robots want to learn about suffixes. In this task, you ar ...

  4. 返回本机的mac物理路径

     /// <summary>         ///    返回本机的mac物理路径         /// </summary>         /// <return ...

  5. Poj3468-A Simple Problem with Integers(伸展树练练手)

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  6. SharePoint excel service web part 连接到 filter web part

    本文讲述SharePoint excel service web part 连接到 filter web part的一个简单应用场景. SharePoint excel service web par ...

  7. execl csv导出

    方维js方法:function export_csv() { var inputs = $(".search_row").find("input:[type!='chec ...

  8. UITableView使用总结和性能优化

    UITableView使用总结和性能优化    UITableView有两种风格:UITableViewStylePlain和UITableViewStyleGrouped.如 果我们查看UITabl ...

  9. python 弄github代码库列表

    1.底        项目要求,征求github的repo的api,为了能够提取repo对数据进行分析. 研究一天.最终克服该问题,較低下.     由于github的那个显示repo的api,列出了 ...

  10. hdu4506小明系列故事——师兄帮帮忙 (用二进制,大数高速取余)

    Problem Description 小明自从告别了ACM/ICPC之后,就開始潜心研究数学问题了,一则能够为接下来的考研做准备,再者能够借此机会帮助一些同学,尤其是美丽的师妹.这不,班里唯一的女生 ...