I have a while loop that populates a dropdown box with values from a mysql table. There are only two matching records and it is repeating them over and over. How do i only display each record once?:
$query = "SELECT * FROM members, sportevents, dates, results, event, userlogin ".
"INNER JOIN members AS m ON userlogin.id = m.id " .
"WHERE userlogin.username = '$un' " .
"AND sportevents.id = members.id " .
"AND sportevents.event_id = event.id " .
"AND sportevents.date_id = dates.id " .
"AND sportevents.result_id = results.id";
echo "<form method='POST' action='display.php'>";
echo "Please Select Your Event<br />";
echo "<select name='event'>";
echo "<option>Select Your Event</option>";
$results = mysql_query($query)
or die(mysql_error());
while ($row = mysql_fetch_array($results)) {
echo "<option>";
echo $row['eventname'];
echo "</option>";
}
echo "</select>";
echo "<input type='submit' value='Go'>";
echo "</form>";
Have you tried running that query manually in the mysql monitor? Nothing in your code would produce an infinite loop, so most likely your query is not doing joins as you expect and is doing a cross-product type thing and creating “duplicate” records.
In particular, your query looks very suspect – you’re using the lazy “from multiple tables” approach, instead of explicitly specifying join types, and you’re using the members table twice (
FROM members ...andINNER JOIN members). You don’t specify a relationship between the original members table and the joined/aliasedmone, so most likely you’re doing amembers * memberscross-product fetch.give that you seem to be fetching only an event name for your dropdown list, you can try eliminating the unused tables – ditch
datesandresults. This will simplify things considerable, then (guessing) you can reduce the query to:I don’t know if the members/userlogins join is necessary – it seems to just feed sportevents.id through to members, but without knowing your DB’s schema, I’ve tried to recreate your original query as best as possible.