You are here

function get_db in Acquia Cloud Site Factory Connector 8.2

Same name and namespace in other branches
  1. 8 acsf_init/lib/cloud_hooks/acquia/db_connect.php \get_db()

Initiates a connection to a specified database.

In some cases, like cloud hooks, we might need to connect to the Drupal database where there is no Drupal bootstrap. For example, we might need to retrieve a drush compatible uri value before we can run a drush command on a site.

Parameters

string $site: The AH site name.

string $env: The AH site environment.

string $db_role: The 'role' of the AH database.

Return value

mysqli mysqli object which represents the connection to a MySQL Server.

1 call to get_db()
uri.php in acsf_init/lib/cloud_hooks/acquia/uri.php

File

acsf_init/lib/cloud_hooks/acquia/db_connect.php, line 37
This file provides helper functions for running Acquia Cloud hooks.

Code

function get_db($site, $env, $db_role) {
  $link = FALSE;
  $creds_file = "/var/www/site-php/{$site}.{$env}/creds.json";
  $creds = json_decode(file_get_contents($creds_file), TRUE);
  if (isset($creds['databases'][$db_role]['db_url_ha']) && is_array($creds['databases'][$db_role]['db_url_ha'])) {
    $db_uri = reset($creds['databases'][$db_role]['db_url_ha']);
    $db_info = url_to_connection_info($db_uri);
    $link = mysqli_connect($db_info['host'], $db_info['username'], $db_info['password'], $db_info['database']) or error('Could not connect: ' . mysqli_connect_error());
  }
  else {
    error('Could not retrieve data from creds.json.');
  }
  return $link;
}