Forum Moderators: coopster

Message Too Old, No Replies

how to select database with php script?

         

shams

1:38 am on Oct 2, 2006 (gmt 0)

10+ Year Member



hi,
i develope a program for a hospital with php and mysql runing with apache server, the thing i need to do is, the user, tables are all the same for eache year, the only deferences will be in the database, i will create one database for each year for example 2006 will be one database name and 2007 other database name, now is it possible with php script when i start the program it ask me whcih database i want to use and as input i put the name of that database, and this will continue for the one session i work or as i wish to change to other database by that script? thinks in advanced for your help?

eelixduppy

5:02 am on Oct 2, 2006 (gmt 0)



Hello shams,

You would use mysql_select_db [us2.php.net] and just have a variable argument. However, make sure that the variables content is a database that actually exists. Example:


$db = $_GET['db'];
$dbs = array('y2004','y2005','y2006');
if(![url=http://us2.php.net/manual/en/function.in-array.php]in_array[/url]($db,$dbs)) {
echo 'Not a valid database!';
[url=http://us2.php.net/manual/en/function.exit.php]exit[/url]();
}
//connect to db
$db = mysql_real_escape_string($db);
mysql_select_db($db);

This is just an example. Good luck!

shams

2:20 am on Oct 3, 2006 (gmt 0)

10+ Year Member



hi eelixduppy,
thanks for reply i will try in post if got any problem.