/** * Get a database connection * * @return AbstractConnection */ public function getInstance() { return $this->factory->newInstance($this->adapter, $this->dsn, $this->username, $this->password); }
/** * init : connect to database * @return boolean Operation result */ public function init() { if (!empty($this->_db)) { return true; } // get system config $svr = AppServer::get_instance(); $db_cfg = $svr->config->get('database', array()); $result = true; try { $this->_debug_mode = isset($db_cfg['debug_mode']) ? $db_cfg['debug_mode'] : false; $connection_factory = new ConnectionFactory(); $db = $connection_factory->newInstance(isset($db_cfg['adapter']) ? $db_cfg['adapter'] : 'mysql', isset($db_cfg['dsn']) ? $db_cfg['dsn'] : 'host=localhost;dbname=mysql', isset($db_cfg['username']) ? $db_cfg['username'] : '******', isset($db_cfg['password']) ? $db_cfg['password'] : ''); $db->connect(); $this->_db = $db; } catch (PDOException $e) { $this->log_last_query(); $result = false; log_message('error', 'Failed to connect to db server : ' . $e->getMessage() . ' [ ' . $e->getCode() . ' ]'); } return $result; }