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

Initialize a database connection.

Note that you can change the mysql_connect() call to mysql_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.mysql.inc, line 54
Database interface code for MySQL database servers.

Code

function db_connect($url) {
  $url = parse_url($url);

  // Check if MySQL support is present in PHP
  if (!function_exists('mysql_connect')) {

    // Redirect to installer if using default DB credentials
    if ($url['user'] == 'username' && $url['pass'] == 'password') {
      include_once 'includes/install.inc';
      install_goto('install.php');
    }
    drupal_maintenance_theme();
    drupal_set_header('HTTP/1.1 503 Service Unavailable');
    drupal_set_title('PHP MySQL support not enabled');
    print theme('maintenance_page', '<p>We were unable to use the MySQL database because the MySQL 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;
  }

  // Decode url-encoded information in the db connection string
  $url['user'] = urldecode($url['user']);

  // Test if database url has a password.
  if (isset($url['pass'])) {
    $url['pass'] = urldecode($url['pass']);
  }
  else {
    $url['pass'] = '';
  }
  $url['host'] = urldecode($url['host']);
  $url['path'] = urldecode($url['path']);

  // Allow for non-standard MySQL port.
  if (isset($url['port'])) {
    $url['host'] = $url['host'] . ':' . $url['port'];
  }

  // - TRUE makes mysql_connect() always open a new link, even if
  //   mysql_connect() was called before with the same parameters.
  //   This is important if you are using two databases on the same
  //   server.
  // - 2 means CLIENT_FOUND_ROWS: return the number of found
  //   (matched) rows, not the number of affected rows.
  $connection = @mysql_connect($url['host'], $url['user'], $url['pass'], TRUE, 2);
  if (!$connection) {

    // Redirect to installer if using default DB credentials
    if ($url['user'] == 'username' && $url['pass'] == 'password') {
      include_once 'includes/install.inc';
      install_goto('install.php');
    }

    // Show error screen otherwise
    drupal_maintenance_theme();
    drupal_set_header('HTTP/1.1 503 Service Unavailable');
    drupal_set_title('Unable to connect to database server');
    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 MySQL database server. This could mean your hosting provider\'s database server is down.</p>
<p>The MySQL error was: ' . theme('placeholder', mysql_error()) . '.</p>
<p>Currently, 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 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;
  }
  if (!mysql_select_db(substr($url['path'], 1))) {
    drupal_maintenance_theme();
    drupal_set_header('HTTP/1.1 503 Service Unavailable');
    drupal_set_title('Unable to select database');
    print theme('maintenance_page', '<p>We were able to connect to the MySQL database server (which means your username and password are okay) but not able to select the database.</p>
<p>The MySQL error was: ' . theme('placeholder', mysql_error($connection)) . '.</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 database name?</li>
  <li>Are you sure the database exists?</li>
  <li>Are you sure the username has permission to access the database?</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;
  }

  /* On MySQL 4.1 and later, force UTF-8 */
  if (version_compare(mysql_get_server_info(), '4.1.0', '>=')) {
    mysql_query('SET NAMES "utf8"', $connection);
  }
  return $connection;
}