Hi,
I'm trying to connect to two databases(users, info) in one script to use the result from a query on one database, as an input for another query on the second database...Is this possible?
Here is the script that I have included in the actual script:
<?php
$dbHostname = "localhost";
$dbUsername = "test";
$dbPassword = "test123";
$dbName = "users";
$dblink = MYSQL_CONNECT($dbHostname, $dbUsername, $dbPassword) OR DIE("Error !! Unable to connect to database");
// Select database or print error message if unsuccessful */
mysql_select_db($dbName) or die( "Unable to select database ".$dbName);
$dbhostname2= "192.168.1.1"; //different machine
$dbusername2 = "test2";
$dbpassword2 = "test123";
$dbname2 = "info";
$dblinkbugs = MYSQL_CONNECT($dbhostname2, $dbusername2, $dbpassword2) OR DIE("Error !! Unable to connect to database");
mysql_select_db($dbname2) or die( "Unable to select database ".$dbname2);
?>
And here is the running script
<?php
include_once("include/db_connection.php");
$query = "SELECT * FROM userdata where contact1email LIKE '%yahoo%' order by username";
$result=mysql_query($query) or die('Query failed: ' . mysql_error());
$num=mysql_numrows($result);
$i=0;
echo $num;
while ($i<$num) {
$username=mysql_result($result,$i,"username");
$firstname=mysql_result($result,$i,"fname");
$lastname=mysql_result($result,$i,"lname");
$contactn=mysql_result($result,$i,"contact1name");
$userid=mysql_result($result,$i,"uid");
$query2 = "select book from booksinfo where uid='$userid'";
$result2= mysql_query($query) or die('Query failed: ' . mysql_error());
$bnum=mysql_numrows($result2);
$bookname=mysql_result($result2,$bnum,"book");
$i++;
}
?>
When I ran the script I got this error message?
Query failed: Table 'info.userdata' doesn't exist
I know that it is using the last added database(which in this case info), and because info does not have userdata I got this error.
But is there are away that I can make it work?
Thank you very much for your help.