PHP/MySQL - Displaying similar results from 2 queries?

  • Thread starter Thread starter Jenny G
  • Start date Start date
J

Jenny G

Guest
Hi,

Im doing a php/mysql project and im pretty new to the languages/database so could do with some help.

I have the following tables:

term(id, term)
product(id, product)
join_table(term_id, product_id)

Basically i have the following code so far:


$product1 = mysql_real_escape_string($_POST['product1']);
$product2 = mysql_real_escape_string($_POST['product2']);

$query1="SELECT * FROM term INNER JOIN join_table ON join_table.term_id=term.id INNER JOIN product ON join_table.product_id=product.id WHERE product = '$product1'";

$query2="SELECT * FROM term INNER JOIN join_table ON join_table.term_id=term.id INNER JOIN product ON join_table.product_id=product.id WHERE product = '$product2'";

$result1=mysql_query($query1) or die(mysql_error());
$result2=mysql_query($query2) or die(mysql_error());

$row1 = mysql_fetch_array($result1);
$row2 = mysql_fetch_array($result2);

while($row1 = mysql_fetch_array($result1)) {
echo $row1['term'];
}

while($row2 = mysql_fetch_array($result2)) {
echo $row2['term'];
}


The user inputs 2 products, the code basically get the id of this product then uses the join table to get the terms id/ids that match this product id, then gets the terms that match the term id/ids.

Whenever you display the results from row1 and row2, you usually get serveral terms being displayed (i think a product_id may have serveral term_ids).

An example of this is:
Row1 - term1, term2, term3, term4
Row2 - term2, term4, term6

In both of the rows term2 and term4 are display. How do i get it so that i only display the terms that match in each row (ie, only display term2 and term4).

This is for a similarity project, so i need to display the similar results or even as a work around, i could even just display how many of the results are the same. In the example above there is 2 similar results.


Any help will be appreciated. Thanks
 
Back
Top