MySQL: determine which database is selected?

后端 未结 8 1223
失恋的感觉
失恋的感觉 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:38

    slightly off-topic (using the CLI instead of PHP), but still worth knowing: You can set the prompt to display the default database by using any of the following

    mysql --prompt='\d> '
    export MYSQL_PS1='\d> '
    

    or once inside

    prompt \d>\_
    \R \d>\_
    
    0 讨论(0)
  • 2020-12-02 05:40

    Just use mysql_query (or mysqli_query, even better, or use PDO, best of all) with:

    SELECT DATABASE() FROM DUAL;
    

    Addendum:

    There is much discussion over whether or not FROM DUAL should be included in this or not. On a technical level, it is a holdover from Oracle and can safely be removed. If you are inclined, you can use the following instead:

    SELECT DATABASE();
    

    That said, it is perhaps important to note, that while FROM DUAL does not actually do anything, it is valid MySQL syntax. From a strict perspective, including braces in a single line conditional in JavaScript also does not do anything, but it is still a valid practice.

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