0

Now that I know that one query will not be able to combine two tables from two different mysql databases, Is it possible to create multiple queries to display results in PHP after combining two tables from two mysql databases? I have two mysql databases that have different passwords. I would like to display the results after I join two tables: one from each database. This is what I have thus far:

$connectdb1 = mysqli_connect("localhost", "dblogin1", "pswd1", "db1");
$connectdb2 = mysqli_connect("localhost", "dblogin2", "pswd2", "db2");

$sorc815sql = "SELECT * FROM db1.myaudtable LEFT JOIN db2.contacttable ON myaudtable.myAudContactID=contacttable.contactID";

$sorc815res = mysqli_query($sorc815sql);
echo '<br>$sv815res='.$sorc815res;

I do get an error, "Warning: mysqli_query() expects at least 2 parameters, 1 given in (file location) on line 31"

cherrie
  • 75
  • 7
  • If this cannot be done in one query, can it be done doing multiple queries? If yes, how? – cherrie Oct 02 '15 at 00:03
  • possible duplicate of [Querying multiple databases at once](http://stackoverflow.com/questions/2132654/querying-multiple-databases-at-once) –  Oct 02 '15 at 00:07
  • Possible duplicate of http://stackoverflow.com/questions/2132654/querying-multiple-databases-at-once. Does your mysql user have permissions for both databases? –  Oct 02 '15 at 00:10
  • yes, see the two mysqli_connect functions in original post – cherrie Oct 02 '15 at 00:19
  • In the connection string you posted, you have appear to be using the database name instead of the username to connect. Read the link and you'll see how to pull from 2 databases in one query. Good luck –  Oct 02 '15 at 00:22
  • My host provider designates the login name the same as the database name. For purposes of this example, I will change. – cherrie Oct 02 '15 at 00:24
  • Oh. Well. Can either user use either database? –  Oct 02 '15 at 00:26

1 Answers1

1

mysqli_query($connection,$query) expects 2 parameters. first one is the connection string, in your case $connectdb1 or $connectdb2 and second query $sorc815sql

You can't join two tables from different database in a single query.

aimme
  • 6,385
  • 7
  • 48
  • 65