最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ZendFramework框架實現(xiàn)連接兩個或多個數(shù)據(jù)庫的方法

 更新時間:2016年12月08日 10:34:17   作者:牛逼的霍嘯林  
這篇文章主要介紹了ZendFramework框架實現(xiàn)連接兩個或多個數(shù)據(jù)庫的方法,涉及ZendFramework框架配置文件與數(shù)據(jù)庫操作相關技巧,需要的朋友可以參考下

本文實例講述了ZendFramework框架實現(xiàn)連接兩個或多個數(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); 

這里是默認的數(shù)據(jù)庫

dao.php調用默認數(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;
}

調用

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

還是會連接默認數(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 經測試 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_;
  } 
}

更多關于zend相關內容感興趣的讀者可查看本站專題:《Zend FrameWork框架入門教程》、《php優(yōu)秀開發(fā)框架總結》、《Yii框架入門及常用技巧總結》、《ThinkPHP入門教程》、《php面向對象程序設計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家基于Zend Framework框架的PHP程序設計有所幫助。

相關文章

  • php之app消息推送案例教程

    php之app消息推送案例教程

    這篇文章主要介紹了php之app消息推送案例教程,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下
    2021-08-08
  • php 使用預處理語句操作數(shù)據(jù)庫

    php 使用預處理語句操作數(shù)據(jù)庫

    今天這篇文章的內容其實也是非?;A的內容,不過在現(xiàn)代化的開發(fā)中,大家都使用框架,已經很少人會去自己封裝或者經常寫底層的數(shù)據(jù)庫操作代碼了。所以這回我們就來復習一下數(shù)據(jù)庫中相關擴展中的預處理語句內容。
    2021-06-06
  • 淺析echo(),print(),print_r(),return之間的區(qū)別

    淺析echo(),print(),print_r(),return之間的區(qū)別

    這篇文章主要是對echo(),print(),print_r(),return之間的區(qū)別進行了詳細的分析介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2013-11-11
  • CentOS 7.2 下編譯安裝PHP7.0.10+MySQL5.7.14+Nginx1.10.1的方法詳解(mini版本)

    CentOS 7.2 下編譯安裝PHP7.0.10+MySQL5.7.14+Nginx1.10.1的方法詳解(mini版

    這篇文章主要介紹了CentOS 7.2 mini版本下編譯安裝PHP7.0.10+MySQL5.7.14+Nginx1.10.1的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • PHP的mysqli_ssl_set()函數(shù)講解

    PHP的mysqli_ssl_set()函數(shù)講解

    今天小編就為大家分享一篇關于PHP的mysqli_ssl_set()函數(shù)講解,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Laravel重寫用戶登錄簡單示例

    Laravel重寫用戶登錄簡單示例

    這篇文章主要介紹了Laravel重寫用戶登錄的方法,結合簡單實例形式分析了Laravel框架根據(jù)已有的login方法重寫實現(xiàn)針對驗證碼、后臺登陸頻率及日志記錄的擴種等功能,需要的朋友可以參考下
    2016-10-10
  • PHP 微信支付類 demo

    PHP 微信支付類 demo

    這篇文章主要介紹了PHP 微信支付類 demo的相關資料,需要的朋友可以參考下
    2015-11-11
  • PHP保留兩位小數(shù)并且四舍五入及不四舍五入的方法

    PHP保留兩位小數(shù)并且四舍五入及不四舍五入的方法

    PHP保留兩位小數(shù)并且四舍五入及保留兩位小數(shù)并且不四舍五入該如何實現(xiàn),不會的朋友可以詳細參考下本文,希望對大家有所幫助
    2013-09-09
  • php實現(xiàn)memcache緩存示例講解

    php實現(xiàn)memcache緩存示例講解

    共享內存是一種在相同機器中的應用程序之間交換數(shù)據(jù)的有效方式,本文說的是php實現(xiàn)memcache緩存示例,大家參考使用吧
    2013-12-12
  • 將博客園(cnblogs.com)數(shù)據(jù)導入到wordpress的代碼

    將博客園(cnblogs.com)數(shù)據(jù)導入到wordpress的代碼

    博客園限制太多,于是決定從博客園(cnblogs)更換自己個人的博客。WORDPRESS口碑還不錯,于是決定用用看。之前發(fā)的數(shù)百篇日志需要導入過來,在網上搜了一會,發(fā)現(xiàn)沒有這個插件,無奈只能自己寫一個
    2013-01-01

最新評論

深水埗区| 阿鲁科尔沁旗| 吴江市| 巍山| 桐城市| 南丹县| 澄城县| 大石桥市| 唐河县| 石城县| 惠州市| 鹤壁市| 许昌市| 阳新县| 会昌县| 武清区| 台北县| 湄潭县| 新建县| 唐海县| 绥阳县| 宿松县| 本溪| 丽江市| 新昌县| 北宁市| 滦南县| 镇雄县| 东兰县| 北安市| 海原县| 蒙阴县| 万山特区| 上高县| 成都市| 淮北市| 涪陵区| 山西省| 台江县| 武城县| 靖州|