Joining two Mysql Queries with php

Basically I have 2 SQL queries from 2 different databases and I am trying to compare where they are equal and then join together the other information for that value. My first query contains an id and a product name, my second query contains a product name and components. So I'm trying to join them on the product name and then show the other two bits of information with them. The db I selected is being used in the second query. Any idea what I should do? So far I have this, which only seems to show one result:

$catid = mysql_query("Select a.entry_id, b.cat_name from blog.exp_category_posts a inner join blog.exp_categories b on a.cat_id=b.cat_id where b.Group_ID = 3");

$results = mysql_query("Select a.name, c.product from wr_scientific a inner join wr_scientific_products b on a.id=b.scienc_id Join xcart_products c on b.prod_id=c.productid LIMIT 1000");

while($row1 = mysql_fetch_array($catid)){
$row2= explode("™", $row1['cat_name']);
$row3= explode("®", $row2[0]);

while($row = mysql_fetch_array($results)){
$rowpro = explode("™", $row['product']);
$rowprod = explode("®", $rowpro[0]);

if($rowprod[0] == $row3[0]){
echo $rowprod[0].$row['name'].$row1['entry_id'];
 }  
  }
 }

Thanks for any help

Answers


If the two databases are located on the same instance of MySQL (~ same machine), then you can refer to a table in say db2 from (say) db1 by prefixing the table name with the database name.

E.g:

USE db1 ;
SELECT
    db1.table_in_db1.id,  -- you can specify the database name here, but
    table_in_db2.id,      -- there is no ambiguity, the database name is optional
    field_in_table_in_db2 -- the same way the table name is optionnal when there is no ambiguity
FROM
    db1.table_in_db1      -- database name is optionnal here, current database is assumed
JOIN
    db2.table_in_db2
ON ...

Need Your Help

Installing & using the Android NDK in Eclipse

android android-ndk installation

I've been running the Android SDK for a while now in Eclipse (MAC OSX). I've downloaded the NDK and installed the C/C++ tools in Eclipse, but could anyone guide me on using the NDK? For example, do I

How to implement MySQLi nested prepared statements?

php mysql mysqli mysqli-multi-query

I am trying to convert MySQL to MySQLi. And I cannot figure out why it brakes on