hi i'm having trouble creating a drop down menu using data from the database and i dont understand where the problem is

<?php
....
$conn ...blah blah blah...
mysql_select_db ...blah blah blah...
....

$query = mysql_query("SELECT * FROM teamdb WHERE sports='Basketball'");
$options = "";
	
while($row = mysql_fetch_array($query))
{
	$teamid = $row["teamid"];
	$teamname = $row["teamname"];
	$options.="<option value=\"$teamid\">".$teamname."</option>";
}

....
?>
<html>
....

<select name='whatever'>
<option value=0> choose</option>
<?=$options?>
</select>

....
</html>

the drop down menu created from this code only has the choose option
and i dont understand whats wrong with it
can anybody help?

and after that is fixed
if i want to put that while loop into a function so i choose which sport to create the menu for.
i just need :
function selectteam ($where)
put the query ... while loop into the function
and change the WHERE field to '$where'
right?

thanks for any help in advance

Dani AI

Generated

A few likely reasons the generated <select> only shows the static "choose" option: the SQL returned no rows, the query failed, the loop never ran because of a syntax/quoting error, or the built options string was never actually printed (short echo tags can be disabled on older PHP). pointed in the right direction by moving the loop into the HTML, but there is a quoting mistake in the example that would produce malformed HTML and suppress options.

Quick troubleshooting checklist:

  • Confirm the DB connection and selection succeed and inspect the query error if any (check the API error function or exception message).
  • Run the same SELECT directly in the DB (phpMyAdmin or mysql client) to confirm rows exist for the filter value.
  • Temporarily dump results inside the loop (for example, print a counter or the fetched row) so you can tell if the loop executes.
  • Use <?php echo $options; ?> if <?= might be unavailable on your PHP version, and make sure the variable is in the same scope where you print it.
  • Escape values with htmlspecialchars() when building options to avoid broken HTML or XSS.

Recommended modern approach (use prepared statements and escape output). Example using PDO:

function buildOptions(PDO $pdo, $sport) {
    $stmt = $pdo->prepare('SELECT id, name FROM teams WHERE sport = :s ORDER BY name');
    $stmt->execute([':s' => $sport]);
    $html = '<option value="">Choose</option>';
    while ($r = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $html .= '<option value="' . htmlspecialchars($r['id'], ENT_QUOTES) . '">'
              . htmlspecialchars($r['name'], ENT_QUOTES) . '</option>';
    }
    return $html;
}

Call this where you render the select: echo buildOptions($pdo, $sportVariable);. Also note mysql_* functions are deprecated and removed in modern PHP—migrate to PDO or mysqli for security and forward-compatibility.

Firstly, to correct your script:

<?php
....
$conn ...blah blah blah...
mysql_select_db ...blah blah blah...
....

$query = mysql_query("SELECT * FROM teamdb WHERE sports='Basketball'");

....
?>
<html>
....

<select name='whatever'>
<?php
while($row = mysql_fetch_array($query))
{
echo '<option value"='.$row['teamid'].'">'.$row['teamname'].'</option>';	

}
?>
</select>

....
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.