Please, don't use mysql_*
functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
While it is theoretically possible to join tables from two different databases on the same server, what you are trying to do cannot possibly work because you appear to be accessing two different servers.
In order to get the result set you want you will need to combine them manually.
For example (using PDO):
$dsn1 = "mysql:host=$host;dbname=$db_name";
$dsn2 = "mysql:host=$host2;dbname=$db_name2";
try {
// Create the connections
$db1 = new PDO($dsn1, $username, $password);
$db1->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db1->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db1->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$db2 = new PDO($dsn2, $username2, $password2);
$db2->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db2->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db2->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
// Get the initial recordset
$sql1 = "
SELECT InterestedEntityId, Score
FROM `user_interests`
WHERE UserId = :userId
ORDER BY Score DESC
";
$stmt1 = $db1->prepare($sql1);
$stmt1->bindParam('userId', $userID, PDO::PARAM_INT);
$stmt1->execute();
// Prepare the statement for the second database
$sql2 = "
SELECT Name
FROM entities
WHERE Id = :entityId
";
$entityId = 0;
$stmt2 = $db2->prepare($sql2);
$stmt2->bindParam('id', $entityId, PDO::PARAM_INT);
// Loop the first result set
$result = array();
foreach ($stmt1 as $row1) {
// Fetch the related data from the second DB
$entityId = $row1['InterestedEntityId'];
$stmt2->execute();
$row2 = $stmt2->fetch();
// Construct the final result row and store it
$result[] = array(
'InterestedEntityId' => $row1['InterestedEntityId'],
'Score' => $row1['Score'],
'Name' => $row2['Name']
);
}
} catch(PDOException $e) {
die($e->getMessage());
}
// The result set you want should now be available
var_dump($result);