db_connect
- Versions
- 4.6 – 6
db_connect($url)
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
Code
includes/database.pgsql.inc, line 52
<?php
function db_connect($url) {
// Check if MySQL support is present in PHP
if (!function_exists('pg_connect')) {
drupal_maintenance_theme();
drupal_set_header('HTTP/1.1 503 Service Unavailable');
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>If you still have to install Drupal, proceed to the <a href="'. base_path() .'install.php">installation page</a>.</p>
<p>If you have already finished installing Drupal, this either means that the username and password information in your <code>settings.php</code> file is incorrect or that we can\'t connect to 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;
}
?>Login or register to post comments 