954,576 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Linking multiple tables from different databases

I currently have a linked table from two tables in one database. I would like to add information from a third table contained in another database. The code for the current table is:

<?php
$con = mysql_connect("","","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("", $con);

$result = mysql_query("SELECT A.Company, A.CStatus, A.OnlineLvl, B.CustomerID, B.Name, C.OrderID, C.DateCall, C.WStatus
FROM A, B, C WHERE A.CompanyID=C.CompanyID and B.CustomerID=C.CustomerID ") or die(mysql_error());

echo "<table border='1'>
<tr>
<th>Company</th>
<th>Company Status</th>
<th>Online Level</th>
<th>Customer ID</th>
<th>Customer Name</th>
<th>Order ID</th>
<th>Date of Call</th>
<th>Order Status</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['Company'] . "</td>";
  echo "<td>" . $row['CStatus'] . "</td>";
  echo "<td>" . $row['OnlineLvl'] . "</td>";
  echo "<td>" . $row['CustomerID'] . "</td>"; 
  echo "<td>" . $row['Name'] . "</td>";
  echo "<td>" . $row['OrderID'] . "</td>";
  echo "<td>" . $row['DateCall'] . "</td>";
  echo "<td>" . $row['WStatus'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
mysql_close(


I would like to add information from another table called 'phonecall' contained in database 'iphone'. I understand that you can call up information by putting the "database.table.row" in the select, from, and where for all the bits of information. However, how should I connect to the databases at this section of code:

mysql_select_db("", $con);


Do I put both database names here?

Thanks for any help!

ptara1
Junior Poster in Training
67 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Since no one replied I continued working on it and this is what I figured out.
In the section I put:

mysql_select_db("database1 and database2", $con);


This worked but I also identified all of the variables by databasename.tablename.row

ptara1
Junior Poster in Training
67 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

That code will fail. But it will not affect your queries if they specify the database names. So you can use it to select one of them, or just remove the line entirely.

Just a sample query:

SELECT * FROM database1.table1 t1, database2.table2 t2 WHERE t1.id = t2.id
pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: