Display Records In A Drop Down Menu Combo Box From A Column Of A Table In PHP

<?php  


$con = mysql_connect('localhost','root','');

$db = 
mysql_select_db('mydb');



//$res = mysql_query("SELECT * FROM songs ORDER BY title") or die ("Invalid query: " . mysql_query());  

//$res = mysql_query ("SELECT * FROM mytable ORDER BY name") or die ("Invalid query: " . mysql_query());  

$res = mysql_query ("SELECT * FROM mytable") or die ("Invalid query: " . mysql_query());  

echo '<label>Select value: </label>'; 
echo '<select id="email" name="email">';  
echo '<option value="">Select Email</option>';  

while ($row = mysql_fetch_assoc($res)) 
{
//     $va = $row['title']; 
    
     $va = $row['email'];     
	echo "<option value='$va'>$va</option>";  
}
  echo '</select>';

?>

Dani AI

Generated

The thread shows the common pattern: fetch rows and print <option> elements. Key improvements that make the code safer, smaller and future-proof: stop using the old mysql_* API (removed in PHP 7+), select only the columns you need, escape output to prevent XSS, enforce a proper charset, and avoid loading huge result sets into arrays when you can stream rows directly.

A minimal, modern pattern (PDO) that keeps HTML simple and escapes output:

<?php
$pdo = new PDO('mysql:host=localhost;dbname=mydb;charset=utf8mb4','dbuser','dbpass',[
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);

$stmt = $pdo->query('SELECT DISTINCT email FROM mytable ORDER BY email');

echo '<label for="email">Select value: </label>';
echo '<select id="email" name="email">';
echo '<option value="">Select Email</option>';

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  $e = htmlspecialchars($row['email'], ENT_QUOTES, 'UTF-8');
  echo "<option value=\"$e\">$e</option>";
}

echo '</select>';

Notes and troubleshooting

  • Use SELECT DISTINCT or GROUP BY to avoid duplicates, and ORDER BY if you want sorted options.
  • Prefer numeric IDs as value and display the readable field when privacy or stability matters (email addresses in a public form can be a data leak).
  • Always escape with htmlspecialchars and set DB/connection charset to utf8mb4 to avoid encoding issues.
  • For very large tables, avoid a giant <select>; use server-side paging or an autocomplete widget instead.

Tying this back to the posts: started the basic approach, showed collecting into arrays (extra memory + an extra loop), and demonstrated building the HTML into a string (useful for separation of concerns). All are valid patterns, but update to PDO/mysqli and add output escaping and charset handling for production code.

Recommended Answers

All 4 Replies

//=======after connected to db =======================
$icount = 0;
$query = "SELECT * FROM something "; 
 $result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result, MYSQL_BOTH)){
$icount++;
$name[$icount] = $row['name'];
$email[$icount] = $row['email'];

}

echo "<select size='1'>";
for ( $counter = 1 ; $counter <= $icount; $counter += 1) {

echo "<option value='$email[$counter]'>$name[$counter]</option>"; 
 }
echo "</select>";
Member Avatar for Member #120589

You're using two loops instead of one. I don't know how that's an improvement.

@kashif
Are you trying to post a solution? Have you got a problem that needs solving? Either way, please put your code in appropriate tags.

2p:

//do connection
$r = mysql_query ("SELECT * FROM mytable") or die ("Invalid query: " . mysql_query()); 
$op = '<label>Select value: </label>\n<select id="email" name="email">\n\t<option value="">Select Email</option>'; 
if(mysql_num_rows($r) > 0){
  while($d = mysql_fetch_array($r)){
    $f = stripslashes($d['title']);
    $op .= "\n\t<option value=\"$f\">$f</option>";
  }
}
$op .= '\n</select>';

i like your way better the other is just easier to understand but correct me if im wrong but in your code you still need to echo the $op for it to be seen

Member Avatar for Member #120589

No you're quite right Hitman. You still need an echo. I place this code in an include file or in a function or above the DTD in a standalone page. I can these just echo $op wherever I need it in the page. This way I keep the bulk of the code separate from the logic (HTML)*. I should say, that this is how I do it - perhaps it's not necessarily the best way.

*OK, I've included HTML in the output string, so I've just contradicted myself. But on balance, I still think it's easier that way.

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.