In my opinion, no it's not.
The reason is mysql_db_query() has been deprecated and we can expect it to be removed from the source tree at some undetermined point in the future. Once it's removed, and you upgrade to the PHP version (or higher) from which it has been removed, you'll just have to change all your code back.
There are 3 solutions which immediately come to mind for connecting to multiple db's in a script run:
- Open a connection for each database.
$ncea = mysql_connect("host", "user", "pass");
mysql_select_db("ncea", $ncea);
$site1 = mysql_connect("host", "user", "pass");
mysql_select_db("ncea", $site1);
// close both connections at end of script
mysql_close($ncea);
mysql_close($site1);
Advantages: Now you just pass the appropriate resource id ($ncea or $site1) into your mysql_query() calls. This will ensure that your query always uses the database it needs.
Drawbacks: You need to open 2 connections instead of just one. This may have a performance hit on your server. Just how much of a hit is impossible to say as it's dependant on a lot of different factors, but if you're dealing with a moderate to relatively large number of page requests I'd stay away from this one.
- Continous calling of mysql_select_db()
This is probably what you're doing right now. Simply call this function to change the active database as dictated by the placement of your queries.
Advantages: Lower server overhead than #1 as you only need to have one open MySQL connection per script execution.
Drawbacks: Requires more code, the code is less elegant and readable, and it's more expensive on the programmer.
- Modify your SQL queries and drop mysql_select_db() all together.
If you specify the database name in the SQL query itself, you don't need to select the database.
ie.
SELECT FROM ncea.foo WHERE bar = 'free beer'
SELECT FROM site1.foo WHERE bar = 'I think the one on the left likes me🙂'
Advantages: Solves your problem of database connection management. It's standard ANSI 92 SQL so it's not likely to change anytime in the near future.
Disadvantages: As with #2, it requires a bit more programmer overhead.
Summary:
Personally, I'd go with #3. It's more elegant and less prone to problems with support or increased server overhead. You're already talking about modifying all your scripts so, unless you've got several hundred queries to edit, it's prolly not going to take you much longer.
HTH.
-geoff