function db_connect in Drupal 6
Same name in this branch
- 6 includes/database.mysqli.inc \db_connect()
- 6 includes/database.mysql.inc \db_connect()
- 6 includes/database.pgsql.inc \db_connect()
Same name and namespace in other branches
- 4 includes/database.mysqli.inc \db_connect()
- 4 includes/database.mysql.inc \db_connect()
- 4 includes/database.pgsql.inc \db_connect()
- 5 includes/database.mysqli.inc \db_connect()
- 5 includes/database.mysql.inc \db_connect()
- 5 includes/database.pgsql.inc \db_connect()
Initialise a database connection.
Note that mysqli does not support persistent connections.
Related topics
1 call to db_connect()
- db_set_active in includes/
database.inc - Activate a database for future queries.
File
- includes/
database.mysqli.inc, line 56 - Database interface code for MySQL database servers using the mysqli client libraries. mysqli is included in PHP 5 by default and allows developers to use the advanced features of MySQL 4.1.x, 5.0.x and beyond.
Code
function db_connect($url) {
// Check if MySQLi support is present in PHP
if (!function_exists('mysqli_init') && !extension_loaded('mysqli')) {
_db_error_page('Unable to use the MySQLi database because the MySQLi extension for PHP is not installed. Check your <code>php.ini</code> to see how you can enable it.');
}
$url = parse_url($url);
// Decode urlencoded 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']);
if (!isset($url['port'])) {
$url['port'] = NULL;
}
$connection = mysqli_init();
@mysqli_real_connect($connection, $url['host'], $url['user'], $url['pass'], substr($url['path'], 1), $url['port'], NULL, MYSQLI_CLIENT_FOUND_ROWS);
if (mysqli_connect_errno() > 0) {
_db_error_page(mysqli_connect_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'])) {
mysqli_query($connection, 'SET NAMES utf8 COLLATE ' . $GLOBALS['db_collation']);
}
else {
mysqli_query($connection, 'SET NAMES utf8');
}
return $connection;
}