Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get the database name currently in use? 1

Status
Not open for further replies.

oceandeep

IS-IT--Management
Jul 6, 2000
69
GR
Dear All,

I am new to PHP.
I have a problem of getting the database name currently in use since my programme needs several databases, and sometimes swaps among them. So that I need to trace which one is currently opened and do the action according to it.
Thank you for your help.

Albert
 
You didn't specify a database server, so I'll answer for MySQL -- it's what I'm most familiar with.

There is no fuction I know of in the PHP mysql_* family of functions that can tell you which is the currently selected database. But MySQL can tell you -- perform the SQL query "SELECT DATABASE()". It will return the name of the current database if one is selected, or blank if one is not.

Write a function which performs "SELECT DATABASE()" then returns the database name. ______________________________________________________________________
Never forget that we are
made of the stuff of stars
 
Thank you very much!
But I don't know how to write the function to to assign the result to a PHP variable. I have tried mysql_result(), it seems doesn't work... because I don't know what the "id" that I should use? Please advise. Thanks a lot!

 
function get_active_db(){

$sql='SELECT DATABASE()';

$sqlresult=mysql_query($sql);

$row=mysql_fetch_row($sqlresult);

$active_db=$row[0];

echo &quot;Active Database :<b> $active_db</b> &quot;;

} ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Wow! It works!
Thank you very very much!
Albert
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top