| 5 database.mysqli.inc | db_connect($url) |
| 5 database.pgsql.inc | db_connect($url) |
| 5 database.mysql.inc | db_connect($url) |
| 6 database.mysqli.inc | db_connect($url) |
| 6 database.mysql.inc | db_connect($url) |
| 6 database.pgsql.inc | db_connect($url) |
Initialise a database connection.
Note that mysqli does not support persistent connections.
Related topics
1 call to db_connect()
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 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']);
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;
}
Login or register to post comments