Get Currently Selected MySQL Database with PHP
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:
- SELECT DATABASE();
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))
- {
- print_r($row);
- }
Once the above has been ran it will then output something like so:
- Array
- (
- [DATABASE()] => my_database
- )
0 comments:
Post a Comment