/** * Establish a connection to the database server * * Connect to the database server and use the Elgg database for a particular database link * * @param string $dblinkname The type of database connection. Used to identify the * resource: "read", "write", or "readwrite". * * @return void * @throws \DatabaseException */ public function establishLink($dblinkname = "readwrite") { $conf = $this->config->getConnectionConfig($dblinkname); // Connect to database $this->dbLinks[$dblinkname] = mysql_connect($conf['host'], $conf['user'], $conf['password'], true); if (!$this->dbLinks[$dblinkname]) { $msg = "Elgg couldn't connect to the database using the given credentials. Check the settings file."; throw new \DatabaseException($msg); } if (!mysql_select_db($conf['database'], $this->dbLinks[$dblinkname])) { $msg = "Elgg couldn't select the database '{$conf['database']}'. Please check that the database is created and you have access to it."; throw new \DatabaseException($msg); } // Set DB for UTF8 mysql_query("SET NAMES utf8"); }
/** * Establish a connection to the database server * * Connect to the database server and use the Elgg database for a particular database link * * @param string $type The type of database connection. "read", "write", or "readwrite". * * @return void * @throws \DatabaseException */ public function connect($type = "readwrite") { $conf = $this->config->getConnectionConfig($type); $params = ['dbname' => $conf['database'], 'user' => $conf['user'], 'password' => $conf['password'], 'host' => $conf['host'], 'charset' => 'utf8', 'driver' => 'pdo_mysql']; try { $this->connections[$type] = DriverManager::getConnection($params); $this->connections[$type]->setFetchMode(\PDO::FETCH_OBJ); } catch (\PDOException $e) { // @todo just allow PDO exceptions // http://dev.mysql.com/doc/refman/5.1/en/error-messages-server.html if ($e->getCode() == 1102 || $e->getCode() == 1049) { $msg = "Elgg couldn't select the database '{$conf['database']}'. Please check that the database is created and you have access to it."; } else { $msg = "Elgg couldn't connect to the database using the given credentials. Check the settings file."; } throw new \DatabaseException($msg); } }