ZendFramework框架實(shí)現(xiàn)連接兩個(gè)或多個(gè)數(shù)據(jù)庫的方法
本文實(shí)例講述了ZendFramework框架實(shí)現(xiàn)連接兩個(gè)或多個(gè)數(shù)據(jù)庫的方法。分享給大家供大家參考,具體如下:
配置文件:
<db> <adapter>PDO_MSSQL</adapter> <config> <host>localhost</host> <port>1433</port> <username>sa</username> <password>123456</password> <dbname>edudb</dbname> <pdoType>sqlsrv</pdoType> </config> </db> <!-- 測試多數(shù)據(jù)庫 --> <db2> <adapter>PDO_MSSQL</adapter> <config> <host>localhost</host> <port>1433</port> <username>sa</username> <password>123456</password> <dbname>test</dbname> <pdoType>sqlsrv</pdoType> </config> </db2>
入口文件
//配置數(shù)據(jù)庫連接 $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);
這里是默認(rèn)的數(shù)據(jù)庫
dao.php調(diào)用默認(rèn)數(shù)據(jù)庫
$db = &$this->getAdapter();
dao2.php連接其他數(shù)據(jù)庫
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; }
調(diào)用
$db = &$this->getAdapter();
還是會連接默認(rèn)數(shù)據(jù)庫。
直接使用
$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']==1){ $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[0], $val[1]); } } //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; } /** * 向表中插入數(shù)據(jù) * array $adata 數(shù)據(jù) * string $table 表名 * int $insterid 是否需要返回插入ID * @return true or false or int */ // @bianding 2013.11.04 更改了pdo中mssql.php的lastInsertId()函數(shù) // @bianding 2013.11.04 經(jīng)測試 mssql.php中的lastInsertId()函數(shù)中的SELECT兩種方式都行 function SaveData($adata, $table, $insterid = 0, $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; } } /** * 刪除表中數(shù)據(jù) * * @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; } } /** * 更新表中數(shù)據(jù) * * @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_; } }
更多關(guān)于zend相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Zend FrameWork框架入門教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《Yii框架入門及常用技巧總結(jié)》、《ThinkPHP入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Zend Framework框架的PHP程序設(shè)計(jì)有所幫助。
- Zend Framework 2.0事件管理器(The EventManager)入門教程
- Zend Framework數(shù)據(jù)庫操作技巧總結(jié)
- Zend Framework數(shù)據(jù)庫操作方法實(shí)例總結(jié)
- Zend Framework入門教程之Zend_Db數(shù)據(jù)庫操作詳解
- Zend Framework教程之連接數(shù)據(jù)庫并執(zhí)行增刪查的方法(附demo源碼下載)
- Zend Framework連接Mysql數(shù)據(jù)庫實(shí)例分析
- 解析如何使用Zend Framework 連接數(shù)據(jù)庫
- zend framework配置操作數(shù)據(jù)庫實(shí)例分析
- windows下zendframework項(xiàng)目環(huán)境搭建(通過命令行配置)
- zend framework多模塊多布局配置
- Zend Framework開發(fā)入門經(jīng)典教程
- ZendFramework2連接數(shù)據(jù)庫操作實(shí)例
相關(guān)文章
Zend Framework教程之Zend_Form組件實(shí)現(xiàn)表單提交并顯示錯(cuò)誤提示的方法
這篇文章主要介紹了Zend Framework教程之Zend_Form組件實(shí)現(xiàn)表單提交并顯示錯(cuò)誤提示的方法,結(jié)合實(shí)例形式詳細(xì)分析了Zend_Form組件的使用方法與實(shí)現(xiàn)表單提交的具體操作步驟,需要的朋友可以參考下2016-03-03ThinkPHP3.2.3框架實(shí)現(xiàn)執(zhí)行原生SQL語句的方法示例
這篇文章主要介紹了ThinkPHP3.2.3框架實(shí)現(xiàn)執(zhí)行原生SQL語句的方法,結(jié)合實(shí)例形式分析了thinkPHP3.2.3框架針對查詢、添加、修改、刪除等原生SQL操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-04-04discuz 首頁四格:最新話題+最新回復(fù)+熱門話題+精華文章插件
discuz 首頁四格:最新話題+最新回復(fù)+熱門話題+精華文章插件...2007-08-08PHP設(shè)計(jì)模式(八)裝飾器模式Decorator實(shí)例詳解【結(jié)構(gòu)型】
這篇文章主要介紹了PHP設(shè)計(jì)模式:裝飾器模式Decorator,結(jié)合實(shí)例形式分析了PHP裝飾器模式Decorator相關(guān)概念、功能、原理、用法及操作注意事項(xiàng),需要的朋友可以參考下2020-05-05關(guān)于DISCUZ不用通行證登陸得內(nèi)容介紹
DISCUZ是中國最常用的論壇,雖然他本身有通行證給大家連接,但實(shí)際上用戶的統(tǒng)一還是很不好,經(jīng)常要建立兩個(gè)用戶表,第一不利于注冊和管理,第二浪費(fèi)數(shù)據(jù)庫。2008-10-10Zend Framework數(shù)據(jù)庫操作技巧總結(jié)
這篇文章主要介紹了Zend Framework數(shù)據(jù)庫操作技巧,結(jié)合實(shí)例形式總結(jié)分析了Zend Framework針對數(shù)據(jù)庫操作的常見函數(shù)、常用操作及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-02-02