db_connect

5 database.mysqli.inc db_connect($url)
5 database.pgsql.inc db_connect($url)
5 database.mysql.inc db_connect($url)
6 database.mysql.inc db_connect($url)
6 database.pgsql.inc db_connect($url)
6 database.mysqli.inc db_connect($url)

Initialize a database connection.

Related topics

1 call to db_connect()

File

includes/database.mysql.inc, line 50
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')) {
    _db_error_page('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.');
  }

  // Decode url-encoded information in the db connection string
  $url['user'] = urldecode($url['user']);
  // Test if database url has a password.
  $url['pass'] = isset($url['pass']) ? urldecode($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 || !mysql_select_db(substr($url['path'], 1))) {
    // Show error screen otherwise
    _db_error_page(mysql_error());
  }

  // Force MySQL to use the UTF-8 character set. Also set the collation, if a
  // certain one has been set; otherwise, MySQL defaults to 'utf8_general_ci'
  // for UTF-8.
  if (!empty($GLOBALS['db_collation'])) {
    mysql_query('SET NAMES utf8 COLLATE ' . $GLOBALS['db_collation'], $connection);
  }
  else {
    mysql_query('SET NAMES utf8', $connection);
  }

  return $connection;
}
Login or register to post comments