Page 1 of 1

Using first database connection as default

Posted: Thu Nov 06, 2008 4:56 am
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!

Re: Using first database connection as default

Posted: Fri Nov 07, 2008 4:05 am
by someberry
Anyone able to help a suffering person out? :cry:

Re: Using first database connection as default

Posted: Fri Nov 07, 2008 5:24 am
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.

Re: Using first database connection as default

Posted: Fri Nov 07, 2008 7:05 am
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.

Re: Using first database connection as default

Posted: Fri Nov 07, 2008 7:33 am
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;
  //...
}