1 database.inc public DatabaseConnection::__construct($dsn, $username, $password, $driver_options = array())

Creates a database connection object.

Parameters for creating a DatabaseConnection are identical to that of creating a PDO object.

@link https://www.php.net/manual/en/pdo.construct.php

Parameters

string $dsn: The Data Source Name, or DSN, contains the information required to connect to the database.

string $username: The user name for the DSN string.

string $password: The password for the DSN string.

array $driver_options: A key => value array of driver-specific connection options.

File

core/includes/database/database.inc, line 335
Core systems for the database layer.

Class

DatabaseConnection
Base Database API class.

Code

public function __construct($dsn, $username, $password, $driver_options = array()) { // phpcs:ignore -- No type hint on $driver_options, add in 2.x.
  // Initialize and prepare the connection prefix.
  $this->setPrefix(isset($this->connectionOptions['prefix']) ? $this->connectionOptions['prefix'] : '');

  // Because the other methods don't seem to work right.
  $driver_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;

  // Call PDO::__construct and PDO::setAttribute.
  $this->pdo = new PDO($dsn, $username, $password, $driver_options);

  // Set a Statement class, unless the driver opted out.
  if (!empty($this->statementClass)) {
    $this->pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS, array(
      $this->statementClass,
      array($this),
    ));
  }
}