API page: http://api.drupal.org/api/drupal/includes--database.inc/function/db_set_...

I'm actually confused with db_set_active documentation.
It says:

Return value
the name of the previously active database or FALSE if non was found.

In my logic mind, I see this sentence as follows:
Return value: the name of the previously active database (IF SUCCEEDED) or FALSE if non was found (FAILED).

So I'm expecting as follows:
db_set_active('bad_database') to return FALSE, because it doesn't exists ("FALSE if non was found")
db_set_active('correct_database') to return my previous database, which is 'default' (BTW. not 'correct_database')

But in practice, db_set_active() never returns FALSE, no matter if requested database exists or not.

See the backtrace:

-> db_set_active($name = 'datagov_wiki_live') sites/all/modules/_custom/data_gov_uk_custom/data_gov_uk_custom.module:1121
  -> is_array('mysql://root:root@localhost/datagov_live') includes/database.inc:135
   >=> FALSE
 => $connect_url = 'mysql://root:root@localhost/datagov_live' includes/database.inc:146
  -> is_array(NULL) includes/database.inc:147
   >=> FALSE
 => $slave_connect_url = NULL includes/database.inc:152 
  -> strpos('mysql://root:root@localhost/datagov_live', '://') includes/database.inc:156
   >=> 5
  -> substr('mysql://root:root@localhost/datagov_live', 0, 5) includes/database.inc:156
   >=> 'mysql' 
 => $db_type = 'mysql' includes/database.inc:156
 => $handler = './includes/database.mysql.inc' includes/database.inc:157
  -> is_file('./includes/database.mysql.inc') includes/database.inc:159
   >=> TRUE
  -> db_connect($url = 'mysql://root:root@localhost/datagov_live') includes/database.inc:166
    -> parse_url('mysql://root:root@localhost/datagov_live') includes/database.mysql.inc:52
     >=> array ('scheme' => 'mysql', 'host' => 'localhost', 'user' => 'root', 'pass' => 'root', 'path' => '/datagov_live')
   => $url = array ('scheme' => 'mysql', 'host' => 'localhost', 'user' => 'root', 'pass' => 'root', 'path' => '/datagov_live') includes/database.mysql.inc:52
    -> function_exists('mysql_connect') includes/database.mysql.inc:55
     >=> TRUE
    -> urldecode('root') includes/database.mysql.inc:60
     >=> 'root'
   => $url['user'] = 'root' includes/database.mysql.inc:60
    -> urldecode('root') includes/database.mysql.inc:62
     >=> 'root'
   => $url['pass'] = 'root' includes/database.mysql.inc:62
    -> urldecode('localhost') includes/database.mysql.inc:63
     >=> 'localhost' 
   => $url['host'] = 'localhost' includes/database.mysql.inc:63
    -> urldecode('/datagov_live') includes/database.mysql.inc:64
     >=> '/datagov_live'
   => $url['path'] = '/datagov_live' includes/database.mysql.inc:64
    -> mysql_connect('localhost', 'root', 'root', TRUE, 2) includes/database.mysql.inc:77
     >=> resource(295) of type (mysql link)
   => $connection = resource(295) of type (mysql link) includes/database.mysql.inc:77
    -> substr('/datagov_live', 1) includes/database.mysql.inc:78
     >=> 'datagov_live'
    -> mysql_select_db('datagov_live') includes/database.mysql.inc:78
     >=> TRUE
    -> mysql_query('SET NAMES utf8', resource(295) of type (mysql link)) includes/database.mysql.inc:90
     >=> TRUE
   >=> resource(295) of type (mysql link)
 => $db_conns['datagov_wiki_live'] = resource(295) of type (mysql link) includes/database.inc:166
 => $previous_name = 'default' includes/database.inc:172 => $active_name = 'datagov_wiki_live' includes/database.inc:174
 => $active_db = resource(295) of type (mysql link) includes/database.inc:175
 >=> 'default'

Test code:

$ drush eval "var_dump(db_set_active('blah'));"
string(7) "default"

Comments

kenorb’s picture

Category: bug » support

Ah, I didn't read:

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.

I found it after some time of finding related issues, i.e.: http://drupal.org/node/269615#comment-886429
So it's not only me: #269615: db_set_active Issue - the {system} table isn't accessible under some conditions.
Is there anyway to make this documentation clearer? Especially in 'Return value' section.

But still it should return FALSE if not exists.

jhodgdon’s picture

Category: support » bug
Status: Active » Closed (duplicate)

The return value doc issue is already filed:
#1184106: db_set_active doc issues