/** * Connect to database server and select database * * @param string $host database host * @param string $user database user * @param string $password database password * @param string $name database name * @param array $params additional connection parameters (name, table prefix) * @return resource database connection identifier, false if error occurred */ function db_initiate($host, $user, $password, $name, $params = array()) { $is_connected = Database::connect($user, $password, $host, $name, $params); if ($is_connected) { Registry::set('runtime.database.skip_errors', false); return true; } return false; }
/** * Connects to database * * @param string $host Database host * @param string $name Database name * @param string $user Database user * @param string $password Database password * @param string $table_prefix Database table prefix * @param string $names Database names for SET NAMES * @return bool Tue on succes connection or already connected, false otherwise */ public function connectToDB($host, $name, $user, $password, $table_prefix, $database_backend, $names = 'utf8') { $connected = true; if ($this->_db_connection == null) { Registry::set('config.table_prefix', $table_prefix); Registry::set('config.database_backend', $database_backend); $connected = Database::connect($user, $password, $host, '', array('table_prefix' => $table_prefix)); if (!$connected) { $this->setNotification('E', '', $this->t('could_not_connect_to_database'), true, 'server_configuration'); } elseif (!Database::changeDb($name)) { // CREATE TABLE SQL command will throw the Fatal error if user does not have the CREATE permissions Registry::set('runtime.database.skip_errors', true); if (!Database::createDb($name)) { $this->setNotification('E', '', $this->t('could_not_create_database'), true, 'server_configuration'); $connected = false; } else { Database::changeDb($name); } Registry::set('runtime.database.skip_errors', false); } if ($connected) { db_query("SET NAMES ?s", $names); db_query("SET sql_mode = ''"); } $this->_db_connection = $connected; } return $connected; }