views:

66

answers:

3

I seem to go back and forth on this, sometimes it works for me, often it doesn't and I can't figure out how/why I can get it to work sometimes, and at other times it won't.

I have 5 databases, and need to grab data from each of them during a maintenance routine. For each of the databases, I have an external file which contains all the db connection code which I include in my page.

The includes look like this

$SGcon = mysql_connect("localhost","root","");

if (!$SGcon)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("sGopen", $SGcon);

For each database I provide a different varaible, so the next database will be

$PTcon = mysql_connect("localhost","root","");

if (!$PTcon)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("pTsecond", $PTcon);

Then when i call my query, I use

 
mysql_query($getQuery,  $PTcon);
mysql_query($secondQuery, $SGcon);

The problem I'm running into is that I'm not getting the correct database used for the query , it seems my script is using the databases which was added to the page last.

Is there a better way for me to switch the database connection to be used?

There is lots of back and forth between the databases as I'm comparing data from one db to data in another, so i'm trying to be efficient with this as well.

Thanks Pete

+1  A: 

I think if you close the db connection in between each query you will solve the problem.

And if you're doing a lot of compares between databases, then I would just use 1 select statement to query all databases at once.

Sev
That's not a terribly good or performant approach. It may not be practical either if you're interleaving database sources on queries for whatever reason.
cletus
I think the problem would be due to caching. Anyway leaving DB's open when not being used isn't performant either.
Sev
Unfortunately it isn't only queries, but updates/inserts as well, so I don't think this would work. Thanks for the feedback though.
pedalpete
UPDATES and INSERTS are also queries.
Sev
Very true Sev, don't know why I was considering these two different things.
pedalpete
+1  A: 

By default, if you pass the same arguments to mysql_connect as a previous call it'll return a cached connection if available, rather than creating a new one. So both queries are running against whichever database you last passed to mysql_select_db.

Passing new_link=true will avoid this behaviour, although database connections can be expensive so you should try to keep as few open as reasonably possible.

The detailed explanation for new_link:

If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters. In SQL safe mode, this parameter is ignored.

+1  A: 

Unless there is a specific reason, you want to keep db connections as low as possible. I would use one connection, and write a method to extend the functionality of the mysql_query() method. You could pass it the SQL string, and the table name then the function calls mysql_select_db($tableName) and re-uses that same connection to process the SQL.

Wade
You're probably right that I could extend the functionality of mysql_query method, but I don't believe I'm at a high enough level of programming to be able too do that. You say mysql_select_db($tableName), I assume you meant $databaseName. But isn't that the same ammount of overhead as new_link=true? I'm not really familiar with the differences between opening and closing connections vs. changing databases.
pedalpete