MySQL: determine which database is selected?

后端 未结 8 1222
失恋的感觉
失恋的感觉 2020-12-02 05:19

After calling mysql_select_db to grab a database, is there any way to later output the name of the database that is currently selected? This seems very basic bu

相关标签:
8条回答
  • 2020-12-02 05:24

    You can always use STATUS command to get to know Current database & Current User

    0 讨论(0)
  • 2020-12-02 05:31
    SELECT DATABASE();
    

    p.s. I didn't want to take the liberty of modifying @cwallenpoole's answer to reflect the fact that this is a MySQL question and not an Oracle question and doesn't need DUAL.

    0 讨论(0)
  • 2020-12-02 05:32

    SELECT DATABASE() worked in PHPMyAdmin.

    0 讨论(0)
  • 2020-12-02 05:33

    Another way for filtering the database with specific word.

    SHOW DATABASES WHERE `Database` LIKE '<yourDatabasePrefixHere>%'
    or
    SHOW DATABASES LIKE '<yourDatabasePrefixHere>%';
    

    Example:

    SHOW DATABASES WHERE `Database` LIKE 'foobar%'
    foobar_animal
    foobar_humans_gender
    foobar_objects
    
    0 讨论(0)
  • 2020-12-02 05:34

    In the comments of http://www.php.net/manual/de/function.mysql-db-name.php I found this one from ericpp % bigfoot.com:

    If you just need the current database name, you can use MySQL's SELECT DATABASE() command:

    <?php
    function mysql_current_db() {
        $r = mysql_query("SELECT DATABASE()") or die(mysql_error());
        return mysql_result($r,0);
    }
    ?>
    
    0 讨论(0)
  • 2020-12-02 05:36
    @mysql_result(mysql_query("SELECT DATABASE();"),0)
    

    If no database selected, or there is no connection it returns NULL otherwise the name of the selected database.

    0 讨论(0)
提交回复
热议问题