Using first database connection as default

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Using first database connection as default

Post by someberry »

Is it possible to tell PHP to use the first database connection as default? I am currently opening the main connection first, but I have another database with just a few odd tables in (which cannot be moved to the main database for security reasons).

Whenever I need to access the secure database I open a new connection. However, after opening this connection I can no longer access the main database.

So, I see I have two options:
1. Post here and see if anyone knows how I can make the first connection the default connection, or
2. Save the first connection into a GLOBAL variable and then add that to each of the queries (I don't really want to have to do this as there are hundreds of queries to change!)

Would there be any performance problems making the connection a GLOBAL variable when there are hundreds/thousands of people on the site?

Thanks for your help!
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Re: Using first database connection as default

Post by someberry »

Anyone able to help a suffering person out? :cry:
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Using first database connection as default

Post by requinix »

There's a third option: after using the second connection, close it. Then the "default" will be the original, first connection.

As for your ideas, you can't do #1.
#2 is certainly a good choice, but apparently it requires modifying your code quite a bit. A "global" variable, as you call it, isn't global across all people on your site, just the running code for that one user. There's no memory/resources drawback to doing that.
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Re: Using first database connection as default

Post by someberry »

tasairis wrote:There's a third option: after using the second connection, close it. Then the "default" will be the original, first connection.
Problem is that it might need be opened multiple times on the same page - I would prefer to not have to open the connection xyz times.
tasairis wrote:#2 is certainly a good choice, but apparently it requires modifying your code quite a bit. A "global" variable, as you call it, isn't global across all people on your site, just the running code for that one user. There's no memory/resources drawback to doing that.
I just meant sticking it in the $GLOBALS variable, I know it wont be available to all users. Ah well, looks like this is the only option.
Hannes2k
Forum Contributor
Posts: 102
Joined: Fri Oct 24, 2008 12:22 pm

Re: Using first database connection as default

Post by Hannes2k »

Hi,
how should php know wich connection to use?
So you have to write a function for telling this php.


The best solution would be to create a special database class, and in the constructor one argument is the main database connection.
If you then call the query method of the class (an object of the class), there is a second optional argument for the database connection.
If you do not provide it, the default db connection, which was provided with the constructor, is used.


It looks like:

Code: Select all

 
$mysql = new database($main_db_connection);
$mysql->query("SELECT * FROM maindatabase");
$mysql->query("SELECT * FROM seconddatabase", $second_database);
 
 
//And mysql::query:
function query($sql, $connection=null) {
  if($connection == null)
       $connection = $this->defaultDbConnection;
  //...
}
Post Reply