public static function open($config) { self::$db = @mysql_connect($config['hostname'], $config['username'], $config['password']); if (self::$db) { @mysql_select_db($config['database']); } }
/** * Abre la conexión. * La configuración debería contener los siguientes campos: * hostname = equipo a cual conectarse * username = usuario * password = clave de acceso * database = nombre de la base de datos * * @param array $config */ public static function connect($config) { self::$db = @new mysqli($config['hostname'], $config['username'], $config['password'], $config['database']); if (self::$db->connect_errno) { throw new Exception(self::$db->connect_error, self::$db->connect_errno); } }
public static function getInstance($option = null) { if (self::$db == null) { self::$db = new Db($option); } return self::$db; }
public static function getInstance() { if (!self::$db) { self::$db = new PDO('mysql:host=' . self::$host . ';dbname=' . self::$dbName . ';port=' . self::$port, self::$user, self::$pass); } return self::$db; }
/** * A Singleton pattern * to prevent multiple use of db * @return object */ public static function __d() { if (!isset($db)) { self::$db = new Db(); } return self::$db; }
public static function init() { $mongo = new Mongo(); self::$db = $db = $mongo->poggin; self::$songs = $db->songs; self::$users = $db->users; }
private function __construct() { try { self::$db = new \PDO("mysql:host=" . DbConfig::DB_HOST . ";dbname=" . DbConfig::DB_NAME, DbConfig::DB_USER, DbConfig::DB_PASS); self::$db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); } catch (\PDOException $e) { echo "Connection Error: " . $e->getMessage(); } }
/** * 切换使用的DB连接 * @param string $sign */ public static function switchDb($sign = 'default') { if (isset(self::$connectList[$sign])) { self::$db = self::$connectList[$sign]; return self::_db(); } else { return false; } }
public static function getInstance() { //static = geen object nodig om aan te roepen if (self::$db != null) { return self::$db; } else { self::$db = new PDO('mysql:host=localhost; dbname=kangu-product', 'root', 'root'); return self::$db; } }
/** * singleton pattern to return single db handle * * @param string db host * @param string db name * @param string db user * @param string db password * @return handle database handle */ protected static function dbConnect($dbHost, $dbName, $dbUser, $dbPassword) { if (!self::$db) { try { self::$db = new PDO('mysql:host=' . $dbHost . ';dbname=' . $dbName, $dbUser, $dbPassword); self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return self::$db; } catch (PDOException $e) { echo '<br />' . __FUNCTION__ . ' in ' . __FILE__ . ' at ' . __LINE__ . ': ' . $e->getMessage() . '<br /><br />'; } } }
public static function change_db($db) { global $dbdata; if ($db != '') { Db::$dbconn = $db; Db::$db = $dbdata[$db]['dbtype']; $conn = Db::conn_db($dbdata); Db::$conn = $conn; } else { echo "Sila semak pilihan pangkalan data"; } }
/** * Database connector */ private function dbConnect() { try { // open connection to MongoDB server $connect = new \Mongo(); // access database Db::$db = $connect->{Db::DB_NAME}; } catch (\MongoConnectionException $e) { die('Error connecting to MongoDB server'); } catch (\MongoException $e) { die('Error: ' . $e->getMessage()); } }
public static function init() { if (!self::$db) { try { $dsn = "mysql:host=" . Config::$db_host . ";dbname=" . Config::$db_name . ";charset=utf8"; self::$db = new PDO($dsn, Config::$db_user, Config::$db_pass); self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); self::$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); } catch (PDOException $e) { exit("Connection error: " . $e->getMessage()); } } return self::$db; }
public static function init() { if (!self::$db) { try { $dsn = 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME; self::$db = new PDO($dsn, DB_USER, DB_PASS); self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); self::$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); } catch (PDOException $e) { die('Connection error: ' . $e->getMessage()); } } return self::$db; }
public static function init() { if (!self::$db) { try { $cfg = Configuracion::getConfiguracion('basedatos'); $dsn = $cfg['dsn']; self::$db = new PDO($dsn, $cfg['usuario'], $cfg['clave']); //Crea un nuevo objeto PDO (PHP Data Object) self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //Indica que lanza una excepción si existe algún error. self::$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); //Establece la propiedad de asociacion para los resultados // de consulta } catch (PDOException $exc) { die('Error de conexion a la base de datos! ' . $exc->getMessage()); } } return self::$db; }
protected function getColumns($tableName = '') { if ($tableName == '') { $tableName = $this->getTable(); } $this->_criteria['sql'] = 'SHOW FULL COLUMNS FROM `' . $tableName . '`'; $dbName = $this->getDb(); Db::db()->{$dbName}->prepare($this->_criteria['sql']); $column = Db::db()->{$dbName}->fetchAll(); $this->reset(); unset($value); foreach ($column as &$value) { $value['dataType'] = $this->_extractType($value['Type']); } return $column; }
public function write($session_id, $session_data) { $dbName = $this->_config['dbName']; $db = Db::db()->{$dbName}; $sql = 'REPLACE INTO `' . $this->_config['tableName'] . '` (`id`, `data`, `expire`) VALUES (:0,:1,:2)'; Db::db()->{$dbName}->prepare($sql); Db::db()->{$dbName}->bindValues(array(array(':0', $session_id), array(':1', $session_data), array(':2', NOW + $this->_gc_maxlifetime))); Db::db()->{$dbName}->execute(); return true; }