As a mock-up example.... if a username and password were sent in via a form it might look something like this:
<?php
// Localize the submitted form values:
$username = (isset($_POST['user']) && !empty($_POST['user'])) ? $_POST['user'] : -1;
$password = (isset($_POST['pass']) && !empty($_POST['pass'])) ? md5($_POST['pass']) : -1;
// If either are not filled in, we just can't work:
if($username === -1 || $password === -1)
die('I just can\'t work like this. You have to give me SOME information!!');
// Connect to our authentication database
$conn = mysql_connect('authentication.domain.com', 'authUser', 'authPass');
if(!$conn)
die('Hmm... seems that we can\'t connect to the mySQL server for authentication.');
mysql_select_db('authentication', $conn);
// Let's authenticate the user:
$query = "SELECT sqlServer, sqlUser, sqlPass, sqlDBName
FROM userAuth
WHERE username='$username'
AND password='$password'";
$result = mysql_query($query);
if(!$result)
die('IMPERSONATOR!!! You are not who you say you are!!');
// Authentication done, let's get some info for the next connect:
$row = mysql_fetch_array($result);
// Free some memory please...
mysql_free_result($result);
mysql_close($conn);
// Now, connect to our second DB:
$conn = mysql_connect($row['sqlServer'], $row['sqlUser'], $row['sqlPass']);
if(!$conn)
die('Misconfiguration!! Check the SQL info in the database.');
mysql_select_db($row['sqlDBName'], $conn);
/*
* From this point, you could use the regular mysql_query() call
* for any subsequent query against the second database.
*/
?>
As an alternative, you can use multiple $conn variables ($conn1, $conn2, $authConn, $compConn, etc.) and have multiple mySQL connections open. Now, this isn't efficient (costs memory) but it can be done. You just have to make sure you specify which database connection you want to use when you call [man]mysql_query/man.