Forum Moderators: coopster

Message Too Old, No Replies

selecting different database

         

jackvull

12:47 pm on Jul 8, 2008 (gmt 0)

10+ Year Member



I have a db.php include file where I set all my connections.
Problem is in some of my code I have to change databases to execute procedures.
This leaves many of my files with code like this:

//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?

eelixduppy

3:26 pm on Jul 8, 2008 (gmt 0)



>> This is a real pain when I move from development to LIVE systems as I have to change all the files.

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.

jackvull

3:32 pm on Jul 8, 2008 (gmt 0)

10+ Year Member



No, the structure is identical.
However, I have a development database and a live database.
When testing I use the DEV one.
But I don't want to have to say DEV in each of the file's.
Can't I just set it in one file and connect using a handle?
mssql_select_db always asks for the database name though - should I be using a variable there instead?

eelixduppy

3:44 pm on Jul 8, 2008 (gmt 0)



>> 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.

[edited by: eelixduppy at 3:54 pm (utc) on July 8, 2008]

bcolflesh

3:47 pm on Jul 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also note that it's a good idea to keep this dp.php file under the webroot for security reasons.

above?

eelixduppy

3:53 pm on Jul 8, 2008 (gmt 0)



>> above?

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.

bcolflesh

4:02 pm on Jul 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Must be a question of semantics - I agree with you - I'd consider "below" the web root to be the sub folders of the publicly accessible web directory.

jackvull

4:03 pm on Jul 8, 2008 (gmt 0)

10+ Year Member




>> 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]

bcolflesh

4:29 pm on Jul 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Add some code to check one of the SERVER variables (maybe SERVER_ADDR or SERVER_NAME):

[us.php.net...]

and use that to determine which db include you will use.

eelixduppy

4:30 pm on Jul 8, 2008 (gmt 0)



I'm not understanding your situation because what you are saying is contradictory. From what I understand you have two database servers, one for developement and the other for your live site. Is this correct?

>> 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 :)