Hello,

I really would like to know how to compare two different databases hosted on the same server, compare each tableA rows with the tableB rows and find out the match and non Match ones using a php Script.
And display the match and non Match ones

Thank you

search for the sql join command and you'll probably find your answer

search for the sql join command and you'll probably find your answer

<?php
$handle_db1 = mysql_connect("localhost","myuser","apasswd");
$handle_db2 = mysql_connect("127.0.0.1","myuser","apasswd");
mysql_select_db("db1",$firstDB);
mysql_select_db("db2",$SecondDB);
//do a query from db1:
$query = "select * from test"; $which = $handle_db1;
mysql_query($query,$which);
//do a query from db2 :
$query = "select * from test"; $which = $handle_db2;
mysql_query($query,$which);
?>

Until there i got The display of both tables from different sql databases now i need to compare those results and display the non match ones

This is the position where i'm now!

<?php
$handle_db1 = mysql_connect("localhost","myuser","apasswd");
$handle_db2 = mysql_connect("127.0.0.1","myuser","apasswd");
mysql_select_db("db1",$firstDB);
mysql_select_db("db2",$SecondDB);
//do a query from db1:
$query = "select * from test"; $which = $handle_db1;
mysql_query($query,$which);
//display result on a table
//do a query from db2 :
$query = "select * from test"; $which = $handle_db2;
//display result on a table
mysql_query($query,$which);
//Merge Both results
$query_join="SELECT FROM SecondDB.TableA a inner join firstDB.TableA  ON TableA.userID=TableB.userID";

?>

Until there i got The display of both tables from different sql databases now i need to compare those results and display the non match ones and match ones after that insert the ones that match on the first database

first, you don't need the first two sql commands anymore
second, I think you should specify what you want to select (in this case *)
third, use the outer join command to get non matches

<?php
$handle_db1 = mysql_connect("localhost","myuser","apasswd");
$handle_db2 = mysql_connect("127.0.0.1","myuser","apasswd");
mysql_select_db("db1",$firstDB);
mysql_select_db("db2",$SecondDB);

//get the results when there is a match
$query_match="SELECT * FROM SecondDB.TableA inner join firstDB.TableA  ON TableA.userID=TableB.userID";

//get the results when there is no match
$query_nomatch="SELECT * FROM SecondDB.TableA outer join firstDB.TableA  ON TableA.userID=TableB.userID";
?>

this should give you the results that do ant don't have a match

first, you don't need the first two sql commands anymore
second, I think you should specify what you want to select (in this case *)
third, use the outer join command to get non matches

<?php
$handle_db1 = mysql_connect("localhost","myuser","apasswd");
$handle_db2 = mysql_connect("127.0.0.1","myuser","apasswd");
mysql_select_db("db1",$firstDB);
mysql_select_db("db2",$SecondDB);

//get the results when there is a match
$query_match="SELECT * FROM SecondDB.TableA inner join firstDB.TableA  ON TableA.userID=TableB.userID";

//get the results when there is no match
$query_nomatch="SELECT * FROM SecondDB.TableA outer join firstDB.TableA  ON TableA.userID=TableB.userID";
?>

this should give you the results that do ant don't have a match

And i can get the result of the queries and insert them on different tables?
Based on your explanation you gave im sure that i can compare all the filds of each table of the database and i also saw it on joint query explanation.
Thank you

And i can get the result of the queries and insert them on different tables?

yes, html tables, sql tables, whatever you want

Based on your explanation you gave im sure that i can compare all the filds of each table of the database and i also saw it on joint query explanation.
Thank you

you're welcome

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.