function Connection::open

Same name in this branch
  1. 11.x core/modules/sqlite/src/Driver/Database/sqlite/Connection.php \Drupal\sqlite\Driver\Database\sqlite\Connection::open()
  2. 11.x core/modules/mysql/src/Driver/Database/mysql/Connection.php \Drupal\mysql\Driver\Database\mysql\Connection::open()
  3. 11.x core/modules/pgsql/src/Driver/Database/pgsql/Connection.php \Drupal\pgsql\Driver\Database\pgsql\Connection::open()
  4. 11.x core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::open()
Same name and namespace in other branches
  1. 9 core/modules/sqlite/src/Driver/Database/sqlite/Connection.php \Drupal\sqlite\Driver\Database\sqlite\Connection::open()
  2. 9 core/modules/mysql/src/Driver/Database/mysql/Connection.php \Drupal\mysql\Driver\Database\mysql\Connection::open()
  3. 9 core/modules/pgsql/src/Driver/Database/pgsql/Connection.php \Drupal\pgsql\Driver\Database\pgsql\Connection::open()
  4. 9 core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::open()
  5. 8.9.x core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php \Drupal\Core\Database\Driver\sqlite\Connection::open()
  6. 8.9.x core/lib/Drupal/Core/Database/Driver/mysql/Connection.php \Drupal\Core\Database\Driver\mysql\Connection::open()
  7. 8.9.x core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php \Drupal\Core\Database\Driver\pgsql\Connection::open()
  8. 8.9.x core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::open()
  9. 10 core/modules/sqlite/src/Driver/Database/sqlite/Connection.php \Drupal\sqlite\Driver\Database\sqlite\Connection::open()
  10. 10 core/modules/mysql/src/Driver/Database/mysql/Connection.php \Drupal\mysql\Driver\Database\mysql\Connection::open()
  11. 10 core/modules/pgsql/src/Driver/Database/pgsql/Connection.php \Drupal\pgsql\Driver\Database\pgsql\Connection::open()
  12. 10 core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::open()

Overrides Connection::open

File

core/modules/mysqli/src/Driver/Database/mysqli/Connection.php, line 58

Class

Connection
MySQLi implementation of \Drupal\Core\Database\Connection.

Namespace

Drupal\mysqli\Driver\Database\mysqli

Code

public static function open(array &$connection_options = []) {
  // Sets mysqli error reporting mode to report errors from mysqli function
  // calls and to throw mysqli_sql_exception for errors.
  // @see https://www.php.net/manual/en/mysqli-driver.report-mode.php
  mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
  // Allow PDO options to be overridden.
  $connection_options += [
    'pdo' => [],
  ];
  try {
    $mysqli = @new \mysqli($connection_options['host'], $connection_options['username'], $connection_options['password'], $connection_options['database'] ?? '', !empty($connection_options['port']) ? (int) $connection_options['port'] : 3306, $connection_options['unix_socket'] ?? '');
    if (!$mysqli->set_charset('utf8mb4')) {
      throw new InvalidCharsetException('Invalid charset utf8mb4');
    }
  } catch (\mysqli_sql_exception $e) {
    if ($e->getCode() === static::DATABASE_NOT_FOUND) {
      throw new DatabaseNotFoundException($e->getMessage(), $e->getCode(), $e);
    }
    elseif ($e->getCode() === static::ACCESS_DENIED) {
      throw new DatabaseAccessDeniedException($e->getMessage(), $e->getCode(), $e);
    }
    throw new ConnectionNotDefinedException('Invalid database connection: ' . $e->getMessage(), $e->getCode(), $e);
  }
  // Force MySQL to use the UTF-8 character set. Also set the collation, if a
  // certain one has been set; otherwise, MySQL defaults to
  // 'utf8mb4_0900_ai_ci' for the 'utf8mb4' character set.
  if (!empty($connection_options['collation'])) {
    $mysqli->query('SET NAMES utf8mb4 COLLATE ' . $connection_options['collation']);
  }
  else {
    $mysqli->query('SET NAMES utf8mb4');
  }
  // Set MySQL init_commands if not already defined.  Default Drupal's MySQL
  // behavior to conform more closely to SQL standards.  This allows Drupal
  // to run almost seamlessly on many different kinds of database systems.
  // These settings force MySQL to behave the same as postgresql, or sqlite
  // in regard to syntax interpretation and invalid data handling.  See
  // https://www.drupal.org/node/344575 for further discussion. Also, as MySQL
  // 5.5 changed the meaning of TRADITIONAL we need to spell out the modes one
  // by one.
  $connection_options += [
    'init_commands' => [],
  ];
  $connection_options['init_commands'] += [
    'sql_mode' => "SET sql_mode = 'ANSI,TRADITIONAL'",
  ];
  if (!empty($connection_options['isolation_level'])) {
    $connection_options['init_commands'] += [
      'isolation_level' => 'SET SESSION TRANSACTION ISOLATION LEVEL ' . strtoupper($connection_options['isolation_level']),
    ];
  }
  // Execute initial commands.
  foreach ($connection_options['init_commands'] as $sql) {
    $mysqli->query($sql);
  }
  return $mysqli;
}

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