Forum Moderators: coopster
//change temporarily
//really need to use the correct handle here from the db.php file but PHP doesn't seem to like it
$selected = mssql_select_db("CCAPP", $dbhandle) or die("Couldn't open database $myDB");//get all of structure to put into the javascript array
$resultStruc = mssql_query("EXEC web_GetStructureForDropDowns") or die(mssql_get_last_message());
$resultControllers = mssql_query("EXEC web_GetCreditControllers") or die(mssql_get_last_message());
//set back
$selected = mssql_select_db("KPI", $dbhandle) or die("Couldn't open database $myDB");
$resultMonths = mssql_query("EXEC kpi_GetMonthData") or die(mssql_get_last_message());
This is a real pain when I move from development to LIVE systems as I have to change all the files.
The best method would be to just change the include db.php file.
Any ideas on how to do this so I can select a connection rather than having to put the db name in all the time?
Why would you have a different database structure on your local machine than on your live server? That doesn't make much sense to me. I'm afraid unless you completely create new connections to the database for each database that you are going to use, you are going to have to stick to the method that you are using above.
Yes, you can. Add the code to something like db.php and require it at the top of your scripts:
require(/path/to/'db.php');
Also note that it's a good idea to keep this db.php file under the webroot for security reasons.
[edited by: eelixduppy at 3:54 pm (utc) on July 8, 2008]
You shouldn't be able to access this file directly from a browser. If for whatever reason PHP stops parsing the file then it will be able to be viewed as plain text, therefore revealing your database credentials to the world. It's best to just keep this file below the web root.
>> Can't I just set it in one file and connect using a handle?Yes, you can. Add the code to something like db.php and require it at the top of your scripts:
require(/path/to/'db.php');
Also note that it's a good idea to keep this db.php file under the webroot for security reasons.
But there are 2 different databases to connect to.
I can't select both at the same time for obvious reasons but don't want to repeat this throughout my code:
$selected = mssql_select_db("CCAPP", $dbhandle) or die("Couldn't open database $myDB");
$selected = mssql_select_db("KPI", $dbhandle) or die("Couldn't open database $myDB");
It's more that I have this spread out between all the files.
When I want to test something, I have to change all the code whereas I just want to change the db.php file.
[edited by: eelixduppy at 4:20 pm (utc) on July 8, 2008]
[edit reason] added formatting [/edit]
[us.php.net...]
and use that to determine which db include you will use.
>> I'd consider "below" the web root to be the sub folders of the publicly accessible web directory.
You are correct. I always get that mixed up. My bad :)