Hi, im new to this site and could do with some php/mysql help for a project i am working on. Im also pretty new to php and im not the best programmer. Basically in my system the user enters the name of two products, then using a mysql query, it finds the terms related to these products. (Each product can have serveral terms and a term can belong to serveral products). Here is the code i have used to do this: $query1="SELECT acc FROM term, association, gene_product WHERE association.term_id=term.id AND association.gene_product_id=gene_product.id AND symbol='$gene1'"; $query2="SELECT acc FROM term, association, gene_product WHERE association.term_id=term.id AND association.gene_product_id=gene_product.id AND symbol='$gene2'"; $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); Code (markup): I basically then just use - echo $row1['acc']; and echo $row2['acc'];to display these terms. It displays them as follows: Product 1 - term1, term2, term3, term4 Product 2 - term2, term4, term6 In the database there is a table that holds the distance between 2 terms. It can work out the distance between 2 terms by doing the following query: $t1="SELECT term1_id FROM graph_path, term WHERE graph_path.term1_id=term.id AND acc = '$goterm1'"; $t2="SELECT term2_id FROM graph_path, term WHERE graph_path.term1_id=term.id AND acc = '$goterm2'"; $result_t1=mysql_query($t1) or die(mysql_error()); $result_t2=mysql_query($t2) or die(mysql_error()); $rowt1 = mysql_fetch_assoc($result_t1); $rowt2 = mysql_fetch_assoc($result_t2); $query="SELECT distance FROM graph_path WHERE term1_id = '$rowt1[term1_id]' AND term2_id = '$rowt2[term2_id]'"; $result=mysql_query($query11) or die(mysql_error()); $row=mysql_fetch_assoc($result11); Code (markup): This was done for when the user enter terms instead of products. What i basically need to do now is find the distance between all the terms from product 1 and all the terms in product 2. By this i mean - i need to take the the 1st term (term 1) from product 1, and get the distance between it and all the terms in product 2, then move onto term 2 in product 1, and get the distance between it and all the terms in product 2... and so on. Im not sure the best way to go about this, i had tried this but it didnt seem to work: while($row1=mysql_fetch_array($result1)) { while($row2=mysql_fetch_array($result2)) { $new_query="SELECT distance FROM graph_path WHERE term1_id = '$result1[term_id]' AND term2_id = '$result2[term_id]'"; $new_result=mysql_query($new_query) or die(mysql_error()); $new_row = mysql_fetch_array($new_result); } } echo $new_row['distance']; Code (markup): I was thinking i maybe i need to store the query results from the products (query1 and query2) into an array, but i havent been successfully in finding a way to do this. Any help with this will be appreciated. Here is the tables of the database that i have been using: product(id, symbol) term(id, acc, type) association(id, term_id, product_id) graph_path(id, term1_id, term2_id, distance) If you's need any more info in order to help me, just ask. Thanks