本文实例讲述了ZendFramework框架实现连接两个或多个数据库的方法。分享给大家供大家参考,具体如下:

配置文件:

 <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>
  <!-- 测试多数据库 -->
  <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>

入口文件

//配置数据库连接
$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

<"<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;
  }
  /**
   * 向表中插入数据
   * 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 = 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;
    }
  }
  /**
   * 删除表中数据
   * 
   * @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_;
  } 
}

更多关于zend相关内容感兴趣的读者可查看本站专题:《Zend FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于Zend Framework框架的PHP程序设计有所帮助。

标签:
ZendFramework,连接,两个,多个,数据库

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。