MySQL:确定选择哪个数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8096550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL: determine which database is selected?
提问by andrewtweber
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 but I couldn't find anything on php.net or stackoverflow (all results are for "no database selected").
调用mysql_select_db
抓取数据库后,有没有什么方法可以稍后输出当前选中的数据库名称?这看起来很基本,但我在 php.net 或 stackoverflow 上找不到任何东西(所有结果都是“没有选择数据库”)。
回答by cwallenpoole
Just use mysql_query (or mysqli_query, even better, or use PDO, best of all) with:
只需使用 mysql_query (或 mysqli_query,甚至更好,或使用 PDO,最好的)与:
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:
关于是否FROM DUAL
应该包括在其中的讨论很多。在技术层面上,它是 Oracle 的遗留物,可以安全地删除。如果您愿意,可以改用以下内容:
SELECT DATABASE();
That said, it is perhaps important to note, that while FROM DUAL
does not actually doanything, 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.
也就是说,可能需要注意的是,虽然FROM DUAL
实际上并没有做任何事情,但它是有效的 MySQL 语法。从严格的角度来看,在 JavaScript 中将大括号包含在单行条件中也没有任何作用,但它仍然是一种有效的做法。
回答by Elijah Lynn
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
.
ps 我不想冒昧地修改@cwallenpoole 的答案以反映这是一个 MySQL 问题而不是 Oracle 问题并且不需要DUAL
.
回答by Mogli
回答by Jan Thom?
In the comments of http://www.php.net/manual/de/function.mysql-db-name.phpI found this one from ericpp % bigfoot.com:
在http://www.php.net/manual/de/function.mysql-db-name.php的评论中,我从 ericpp % bigfoot.com 找到了这个:
If you just need the current database name, you can use MySQL's SELECT DATABASE() command:
如果你只需要当前的数据库名称,你可以使用 MySQL 的 SELECT DATABASE() 命令:
<?php
function mysql_current_db() {
$r = mysql_query("SELECT DATABASE()") or die(mysql_error());
return mysql_result($r,0);
}
?>
回答by Jan Thom?
@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.
如果没有选择数据库,或者没有连接,则返回NULL
所选数据库的名称。
回答by Jeff
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
稍微偏离主题(使用 CLI 而不是 PHP),但仍然值得了解:您可以使用以下任何一种方式设置提示以显示默认数据库
mysql --prompt='\d> '
export MYSQL_PS1='\d> '
or once inside
或一旦进入
prompt \d>\_
\R \d>\_
回答by Dexter
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
回答by Abhi Chavda
SELECT DATABASE()
worked in PHPMyAdmin.
SELECT DATABASE()
在 PHPMyAdmin 工作。