Is there a way to get Drupal to tell me the name of the database it's using? I can't see it listen on the status screen.

I've just upgraded both my main site and staging site from 5.2 to 5.3, thought I'd got the hang of all the different settings.php files and database names ... but I'd like to check each one is using the right DB, just to be sure.

Comments

rogerpfaff’s picture

this information is viewable in the settings.php file

joachim’s picture

Is there no way to get the system to output it, just to confirm?
I messed up changing the user name instead of the database name in that so I'd like to doublecheck.

rogerpfaff’s picture

id on't think there is another way than by settings.php. perhaps the developer module could give you this kind of information but i'm not into this module. but where is the problem to compare settings.php with phpmyadmin data?

vm’s picture

The devel.module doesn't do this either. Drupal doesn't double check that your settings are correct. It depends on them being correct.

use phpmyadmin or whatever your hosts DB tool of choice is and verify the necessary information comared to your settings.php file.

joachim’s picture

because I keep mixing up the database name and the mySQL user in the line in settings.php and also nearly managed to edit the wrong settings.php (staging site instead of production site) in my FTP... so I'd like a way to make sure! :)

vm’s picture

The way to make sure it to verify against your hosts settings.

rogerpfaff’s picture

how did you install your staging and production site?

is this one multisite drupal installation with two databases or are there two different installations with two databases?

joachim’s picture

They're two different installations, one in my webhost's root and one in a subfolder.
Then on my local machine I have a bunch of drupals for various things (mucking about with new modules and theming, a staging site that's a close copy of my production site, a totally clean 5.3 I can copy and test things on, and one with the station.module CVS HEAD for making patches)

Finding the settings.php that matches, and being sure I've got the right one is a pain -- they all look the same. But for each drupal I've set a different theme and title, so in my browser I KNOW what I'm looking at. Hence, getting drupal to tell me what DB it's using makes life easier.

joachim’s picture

Got it done...

The following in a dummy module's modulename.install adds the information to the admin status page:

<?php
// $Id$

function dbname_requirements($phase) {
  $requirements = array();
  
  // Ensure translations don't break at install time
  $t = get_t();
  
  global $db_url;
  
  $url = parse_url($db_url);

  // Report Drupal version
  if ($phase == 'runtime') {
    $requirements['dbname'] = array(
      'title' => $t('Database name'),
      'value' => substr($url['path'], 1),
      'severity' => REQUIREMENT_INFO,
      'weight' => 0,
    );
    $requirements['dbuser'] = array(
      'title' => $t('Database user'),
      'value' => $url['user'],
      'severity' => REQUIREMENT_INFO,
      'weight' => 0,
    );
  }

  return $requirements;
}

... though I don't know why this isn't in there already.
Would anyone else find this useful? I could bundle it as a proper module.

aloe’s picture

I am trying to get this to work in Drupal 7. This module would be a great tool for me! I have it setup and enabled in the module list but can't seem to view the database name anywhere...

Quint’s picture

This will do it for you:

Create a block,
select input type of PHP
paste in this code

<?php 
$sql = 'show databases';
$result = db_result(db_query( $sql ));
print "$result";
?>

select where to display the block
set the visiblity settings for only you, or admins, or whatever, so anons can't see it

Quint

joachim’s picture

Thanks!
I've got it showing in the status page -- see above :)

truijens’s picture

In case anyone else is trying to achieve this for newer Drupal versions. For Drupal 7 the above suggestions do not work. However, there is a global variable called $databases. Which should be helpful. It contains all the database information.

    global $databases;

    echo '<pre>';
    print_r($databases);
    echo '</pre>';
alpiniste’s picture

Yes, I have been trying with Drupal 7, yet haven't found the way to get the current database name…
The global variable $databases contains all the registered database information. However, modules etc can switch between them. So, if there are more than one database, you still don't know which one is currently being used!
(Actually what I want is the current prefix of the tables. If I find the current database name, I can use $databases to find it out...)
Any help will be appreciated!

jaypan’s picture

You shouldn't need to know this information. Drupal handles all this for you. Since you are asking about it, I'd guess you are maybe trying to do something in a non-optimal manner. What is it that you are trying to do?

Contact me to contract me for D7 -> D10/11 migrations.

alpiniste’s picture

Thank you Jaypan for the comment!
I think I now understand what you mean.
I just wanted to access one of Drupal tables from my module. It was working, but then I realised it might not in another environment, where the table prefix is different from my environment.
Now I have found users do not need know or explicitly specify the table prefix to access the table from PHP, if the table name given is enclosed with curly brackets like

  $result = db_query_range('SELECT n.title FROM {node} n', 0, 20);

So, as far as my imminent problem is concerned, it is now solved (I think). Thank you again for your kind offer of help, Jaypan!

jaypan’s picture

That's correct. By using the curly braces, Drupal will insert your table prefixes, and you do not have to worry about it!

Contact me to contract me for D7 -> D10/11 migrations.

shaktik’s picture

    [default] => Array
        (
            [default] => Array
                (
                    [database] => database_name
                    [username] => database_username
                    [password] => database_password
                    [host] => database_host
                    [port] => 3311
                    [driver] => mysql
                    [prefix] => prefix_
                )

        )

)
jonathanshaw’s picture

drush sql-query "SHOW DATABASES"
mmjvb’s picture

in information_schema. To figure out the currently connected database you could use:

select table_schema from information_schema.tables where table_name = 'sessions';

Use '{sessions}' to include the prefix in the name of the table. Showed sessions, but you could use any known Drupal table.

As an alternative you could use SHOW OPEN TABLES;
The first column, Database, shows the Database in use.

damondt’s picture

drush status will output this information

stevenlafl’s picture

In code for D8: 

\Drupal::database()->getConnectionOptions()['database']
bardiuk’s picture

drush eval "echo \Drupal::database()->getConnectionOptions()['database'], \"\n\""