Error message

You are browsing documentation for drupal 7.x, which is not supported anymore. Read the updated version of this page for drupal 11.x (the latest version).

function Database::openConnection

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database::openConnection()
  2. 10 core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database::openConnection()
  3. 9 core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database::openConnection()
  4. 8.9.x core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database::openConnection()

Opens a connection to the server specified by the given key and target.

Parameters

$key: The database connection key, as specified in settings.php. The default is "default".

$target: The database target to open.

Throws

DatabaseConnectionNotDefinedException

DatabaseDriverNotSpecifiedException

1 call to Database::openConnection()
Database::getConnection in includes/database/database.inc
Gets the connection object for the specified database key and target.

File

includes/database/database.inc, line 1777

Class

Database
Primary front-controller for the database system.

Code

final protected static function openConnection($key, $target) {
 if (empty(self ::$databaseInfo)) {
 self ::parseConnectionInfo ();
 }
 // If the requested database does not exist then it is an unrecoverable
 // error.
 if (!isset(self ::$databaseInfo[$key])) {
 throw new DatabaseConnectionNotDefinedException ('The specified database connection is not defined: ' . $key);
 }
 if (!$driver = self ::$databaseInfo[$key][$target]['driver']) {
 throw new DatabaseDriverNotSpecifiedException ('Driver not specified for this database connection: ' . $key);
 }
 // We cannot rely on the registry yet, because the registry requires an
 // open database connection.
 $driver_class = 'DatabaseConnection_' . $driver;
 require_once DRUPAL_ROOT  . '/includes/database/' . $driver . '/database.inc';
 $new_connection = new $driver_class(self ::$databaseInfo[$key][$target]);
 $new_connection->setTarget ($target);
 $new_connection->setKey ($key);
 // If we have any active logging objects for this connection key, we need
 // to associate them with the connection we just opened.
 if (!empty(self ::$logs[$key])) {
 $new_connection->setLogger (self ::$logs[$key]);
 }
 return $new_connection;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.