You are here

function db_connect in Drupal 4

Same name in this branch
  1. 4 includes/database.mysqli.inc \db_connect()
  2. 4 includes/database.mysql.inc \db_connect()
  3. 4 includes/database.pgsql.inc \db_connect()
Same name and namespace in other branches
  1. 5 includes/database.mysqli.inc \db_connect()
  2. 5 includes/database.mysql.inc \db_connect()
  3. 5 includes/database.pgsql.inc \db_connect()
  4. 6 includes/database.mysqli.inc \db_connect()
  5. 6 includes/database.mysql.inc \db_connect()
  6. 6 includes/database.pgsql.inc \db_connect()

Initialize a database connection.

Note that you can change the pg_connect() call to pg_pconnect() if you want to use persistent connections. This is not recommended on shared hosts, and might require additional database/webserver tuning. It can increase performance, however, when the overhead to connect to your database is high (e.g. your database and web server live on different machines).

Related topics

1 call to db_connect()
db_set_active in includes/database.inc
Activate a database for future queries.

File

includes/database.pgsql.inc, line 22
Database interface code for PostgreSQL database servers.

Code

function db_connect($url) {

  // Check if MySQL support is present in PHP
  if (!function_exists('pg_connect')) {
    drupal_maintenance_theme();
    drupal_set_title('PHP PostgreSQL support not enabled');
    print theme('maintenance_page', '<p>We were unable to use the PostgreSQL database because the PostgreSQL extension for PHP is not installed. Check your <code>PHP.ini</code> to see how you can enable it.</p>
<p>For more help, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.</p>');
    exit;
  }
  $url = parse_url($url);
  $conn_string = '';

  // Decode url-encoded information in the db connection string
  if (isset($url['user'])) {
    $conn_string .= ' user=' . urldecode($url['user']);
  }
  if (isset($url['pass'])) {
    $conn_string .= ' password=' . urldecode($url['pass']);
  }
  if (isset($url['host'])) {
    $conn_string .= ' host=' . urldecode($url['host']);
  }
  if (isset($url['path'])) {
    $conn_string .= ' dbname=' . substr(urldecode($url['path']), 1);
  }
  if (isset($url['port'])) {
    $conn_string .= ' port=' . urldecode($url['port']);
  }

  // pg_last_error() does not return a useful error message for database
  // connection errors. We must turn on error tracking to get at a good error
  // message, which will be stored in $php_errormsg.
  $track_errors_previous = ini_get('track_errors');
  ini_set('track_errors', 1);
  $connection = @pg_connect($conn_string);
  if (!$connection) {
    drupal_maintenance_theme();
    drupal_set_header('HTTP/1.1 503 Service Unavailable');
    drupal_set_title('Unable to connect to database');
    print theme('maintenance_page', '<p>This either means that the database information in your <code>settings.php</code> file is incorrect or we can\'t contact the PostgreSQL database server. This could mean your hosting provider\'s database server is down.</p>
<p>The PostgreSQL error was: ' . theme('placeholder', decode_entities($php_errormsg)) . '</p>
<p>Currently, the database is ' . theme('placeholder', substr($url['path'], 1)) . ', the username is ' . theme('placeholder', $url['user']) . ', and the database server is ' . theme('placeholder', $url['host']) . '.</p>
<ul>
  <li>Are you sure you have the correct username and password?</li>
  <li>Are you sure that you have typed the correct hostname?</li>
  <li>Are you sure you have the correct database name?</li>
  <li>Are you sure that the database server is running?</li>
</ul>
<p>For more help, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.</p>');
    exit;
  }

  // Restore error tracking setting
  ini_set('track_errors', $track_errors_previous);
  return $connection;
}