shane shane - 22 days ago 7
MySQL Question

query result echoing results four times

I have a functioning query that is echoing the correct results, for 2 people in the reservation, except that it publishing the results four times instead of just once.

Echo of results should be:

Total for Options: $94

Instead, the echo is:

Total for Options: $94

Total for Options: $94

Total for Options: $94

Total for Options: $94

Here's the associated query and PHP code:

$optquery = "SELECT t.ibtp, c.ibt
FROM clients c, tourprices t
WHERE c.tourstart = t.tourstart
AND c.tourbk_id = t.tour_id";

$optresult = $db->query($optquery);

while ($optrate = mysqli_fetch_assoc($optresult))
{

$opttotalcost = $optrate['ibtp'] * $optrate['ibt'];
echo '<strong>',"Total for Options :</strong> $ {$opttotalcost}<br />";
}


Any one see the problem?

Answer

You may have four identical (for those columns) rows in your database. Try using SELECT DISTINCT, as follows:

$optquery = "SELECT DISTINCT t.ibtp, c.ibt
                FROM clients c, tourprices t 
                WHERE c.tourstart = t.tourstart
                AND c.tourbk_id = t.tour_id";