MySQL:确定选择哪个数据库? [英] MySQL: determine which database is selected?

查看:96
本文介绍了MySQL:确定选择哪个数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调用mysql_select_db来获取数据库之后,有什么方法可以稍后输出当前所选数据库的名称?这似乎很基础,但是我在php.net或stackoverflow上找不到任何东西(所有结果都是针对未选择数据库").

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_query(或mysqli_query甚至更好,或使用PDO最好)与以下各项配合使用:

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

SELECT DATABASE() FROM DUAL;


附录:

关于是否应在其中包含FROM DUAL的讨论很多.从技术上讲,它是Oracle的保留,可以安全地删除.如果您愿意,可以改用以下内容:


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();

也就是说,可能需要注意的是,虽然FROM DUAL实际上并没有做什么,但它是有效的MySQL语法.从严格的角度来看,在JavaScript中以单行包含大括号也不会执行任何操作,但这仍然是一种有效的做法.

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.

这篇关于MySQL:确定选择哪个数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆