| 5 database.inc | db_set_active($name = 'default') |
| 6 database.inc | db_set_active( |
| 7 database.inc | db_set_active($key = 'default') |
| 8 database.inc | db_set_active($key = 'default') |
Activate a database for future queries.
If it is necessary to use external databases in a project, this function can be used to change where database queries are sent. If the database has not yet been used, it is initialized using the URL specified for that name in Drupal's configuration file. If this name is not defined, a duplicate of the default connection is made instead.
Be sure to change the connection back to the default when done with custom code.
Parameters
$name: The key in the $db_url global variable from settings.php. If omitted, the default connection will be made active.
Return value
The name of the previously active database, or FALSE if none was found.
Related topics
3 calls to db_set_active()
File
- includes/
database.inc, line 124 - Wrapper for database interface code.
Code
function db_set_active($name = 'default') {
global $db_url, $db_type, $active_db;
static $db_conns, $active_name = FALSE;
if (empty($db_url)) {
include_once 'includes/install.inc';
install_goto('install.php');
}
if (!isset($db_conns[$name])) {
// Initiate a new connection, using the named DB URL specified.
if (is_array($db_url)) {
$connect_url = array_key_exists($name, $db_url) ? $db_url[$name] : $db_url['default'];
}
else {
$connect_url = $db_url;
}
$db_type = substr($connect_url, 0, strpos($connect_url, '://'));
$handler = "./includes/database.$db_type.inc";
if (is_file($handler)) {
include_once $handler;
}
else {
_db_error_page("The database type '" . $db_type . "' is unsupported. Please use either 'mysql' or 'mysqli' for MySQL, or 'pgsql' for PostgreSQL databases.");
}
$db_conns[$name] = db_connect($connect_url);
}
$previous_name = $active_name;
// Set the active connection.
$active_name = $name;
$active_db = $db_conns[$name];
return $previous_name;
}
Login or register to post comments
Comments
how to run query on different database
You can use following code to add and connect to the new database:
<?php$db_name = 'my_new_database'; // database name to which you want to connect
global $db_url;
if (is_string($db_url)) {
$db_url = array('default' => $db_url);
$prevdb = end(explode('/', current($db_url)));
}
$db_url[$db_name] = substr($db_url['default'], 0, -strlen($prevdb)) . $db_name;
if (db_set_active($db_name)) {
// HERE YOU CAN OPERATE ON YOUR NEW DATABASE
db_query("SELECT 1");
}
db_set_active();
?>
Can this break search or indexing of the site
Hi,
We are using db_set_active function and it works perfectly we can go from Drupal database to our internal database.
What we are seeing is that we cant index our site anymore
when we try to invoke the search from supercron we are getting error and the apache process child pid 1296 exit signal Segmentation fault (11)
Here is the error that we get in the browser
Fatal error: Call to undefined function db_query_array() in /var/www//html/includes/common.inc(1696) : eval()'d code on line 3 Warning: Table 'OURBD.system' doesn't exist query: SELECT * FROM system WHERE type = 'theme' in /var/www/html/includes/database.mysqli.inc on line 134 Notice: Undefined index: simple_custom in /var/www/html/includes/theme.inc on line 71 Notice: Trying to get property of non-object in /var/www/html/includes/theme.inc on line 104 Notice: Trying to get property of non-object in /var/www//html/includes/theme.inc on line 231 Warning: Table 'OURDB.cache' doesn't exist query: SELECT data, created, headers, expire, serialized FROM cache WHERE cid = 'theme_registry:' ....
It is like the search and index function is trying to search our own DB instead of only the default drupal DATABASE.
What we have tried
Installed a module to restrict seach
and on node were we use the function db_set_active we select
X Restrict this node from the search index
But we have no success until now.
We would prefer an option to only ignore this db_set_active from the search.
We are using the them simple_custom don't know if it is related to our theme but we don't think so.
Any suggestion on how to user db_set_active and to not break the search of the site ?
Maybe we could create the table needed for the search in our internal database ?
Thanks.
i think we found something we
i think we found something we had node with invalid code in it, cron stil doest work but we dont have this error anymore.
Bye