0

I am trying create a order history page. An SQL statement that finds all the applications based on a order_ID in an orderdetails_tbl. Here is the SQL.

SELECT * FROM applications_tbl INNER JOIN orderdetails_tbl ON orderdetails_tbl.app_ID = applications_tbl.app_ID WHERE orderdetails_tbl.order_ID = $orderID

The results are displayed using this PHP.

$result = mysql_query("SELECT * 
FROM applications_tbl
INNER JOIN orderdetails_tbl ON orderdetails_tbl.app_ID = applications_tbl.app_ID WHERE orderdetails_tbl.order_ID = ".$order_ID."");
$row = mysql_fetch_assoc($result);
echo "<table>";
do { ?>

    <tr style="background-color:#fff">
    <td> <img src="getimage.php?ID=<?php echo $row['app_ID']; ?>" width="100" height="100" alt="IMAGE" /></td>
           <td width="20%"><?php echo $row['app_name']; ?></td>
           <td><?php echo $row['app_desc']; ?></td>
           <td width="15%"><?php echo $row['app_cost']; ?></td>
     </tr>    
     <?php  
 } while ($row = mysql_fetch_assoc($result)); 

The SQL returns the correct values and the PHP displays them correctly. My issue is that it on the webpage it returns many duplicate results. It appears to be random, but I noticed the more apps/items in the order the more duplicate results. In phpMyAdmin when I run the SQL it doesn't display duplicates.

Cheers for your time. Any advice will be greatly received!

4
  • can you show us the duplicate records? Commented Apr 7, 2013 at 11:14
  • try mysql_fetch_row Commented Apr 7, 2013 at 11:17
  • orderdetails_tbl sounds like it holds the line items of the order. You should have another order table for the head of the order. Otherwise, you could just use SELECT DISTINCT. Commented Apr 7, 2013 at 11:18
  • 1
    Your should not use SELECT * on the table. Specify the fields you need and then also add a DISTINCT statement Commented Apr 7, 2013 at 11:18

2 Answers 2

1

Use GROUP BY

$result = mysql_query("SELECT * FROM applications_tbl
INNER JOIN orderdetails_tbl ON orderdetails_tbl.app_ID = applications_tbl.app_ID 
WHERE orderdetails_tbl.order_ID = ".$order_ID." GROUP BY app_ID");
Sign up to request clarification or add additional context in comments.

Comments

0

A tip is to use alias to get sql-statements more readable like this:

$result = mysql_query("SELECT * FROM applications_tbl at INNER JOIN orderdetails_tbl od ON od.app_ID = at.app_ID WHERE od.order_ID = ".$order_ID);

But to answer your question you could use DISTINCT on one field and list the fields you want to have included in your recordset or add a GROUP BY -statement at the end of the SQL-statement.

DISTINCT: (example)

$result = mysql_query("SELECT DISTINCT at.id, od.app_ID FROM applications_tbl at INNER JOIN orderdetails_tbl od ON od.app_ID = at.app_ID WHERE od.order_ID = ".$order_ID);

OR

GROUP BY (example)

$result = mysql_query("SELECT * FROM applications_tbl at INNER JOIN orderdetails_tbl od ON od.app_ID = at.app_ID WHERE od.order_ID = ".$order_ID . " GROUP BY at.id");

What is the actual value of $order_ID ? (I ask because you say you have the same query and you only get duplicates when viewing in browser. It seems very odd)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.