예제 #1
0
<?php

require_once __DIR__ . '/pdolb.php';
$pdo = PDOLB::getInstance();
$stmt = $pdo->prepare('show tables');
$stmt->execute(array());
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($rows);
$stmt->closeCursor();
$stmt = null;
$pdo = null;
예제 #2
0
파일: pdolb.php 프로젝트: xianhuazhou/PDOLB
 /**
  *
  * get a random PDOLB instance
  *
  * @param int $type
  *
  * @access private
  * @return PDOLB
  *
  */
 private static function getRandomPDOLB($type = self::MASTER_SLAVE)
 {
     switch ($type) {
         case self::MASTER_SLAVE:
             $databases = array_merge(self::$databases['master'], self::$databases['slave']);
             break;
         case self::MASTER:
             $databases = self::$databases['master'];
             break;
         case self::SLAVE:
             $databases = self::$databases['slave'];
             break;
     }
     self::shuffleDatabases($databases);
     $pdolb = null;
     $exceptions = array();
     foreach ($databases as $database) {
         if (isset(self::$connections[$database['dsn']])) {
             return self::$connections[$database['dsn']];
         }
         try {
             $pdolb = new PDOLB($database['dsn'], $database['username'], $database['password'], $database['driver_options']);
         } catch (Exception $e) {
             $exceptions[$database['dsn']] = $e->getMessage();
             continue;
         }
         if ($pdolb && '' == $pdolb->errorCode()) {
             return self::$connections[$database['dsn']] = $pdolb;
         } else {
             $exceptions[$database['dsn']] = $pdolb->errorInfo();
         }
     }
     throw new PDOLBException("No available database!\n" . print_r($exceptions, true));
 }