Get Currently Selected MySQL Database with PHP

July 1st, 2014 - Posted by Steve Marks to MySQL, PHP, Web Development.

When debugging a PHP script, or when your site contains multiple MySQL connections, it can be useful to know which database your PHP script is currently connected to.

In MySQL you can simply run the following query:


Pretty easy right? We can do exactly the same thing in PHP by taking the above SQL query and extracting the result like so:

$query = "SELECT DATABASE();";
$result = mysql_query($query) or die(mysql_error());
if ($row = mysql_fetch_assoc($result))

Once the above has been ran it will then output something like so:

    [DATABASE()] => my_database
Tags: ,
This entry was posted on Tuesday, July 1st, 2014 at 8:15 pm by +Steve Marks and is filed under MySQL, PHP, Web Development. You can follow any responses to this entry through the RSS 2.0 feed.

Fear not, we won't publish this

Comments (0)

No comments have been left yet. Be the first