By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,406 Members | 2,660 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,406 IT Pros & Developers. It's quick & easy.

retrieve data from mysql database to display in a dropdown box with php

P: 1
Hello everybody,
I am quite beginner as a software professional in php & mysql. I want to know how I can fetch data from mysql database to show in a form's drop down box with php coding. I request all to help me in this regard.
Sincerely,
Vireshwar,
Chennai,India
May 22 '07 #1
Share this Question
Share on Google+
1 Reply


P: 1
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3.  
  4. <?php
  5.  
  6. $con=mysql_connect("localhost","root","") 
  7. or die('Could not connect: ' . mysql_error());
  8.  
  9.  
  10. mysql_select_db("my_db", $con);
  11.  
  12. $result = mysql_query("SELECT username FROM users")
  13. or die("Invalid Query: " .mysql_query());
  14.  
  15. echo '<label>Select username:</label>';
  16. echo '<select id="username"  name="username">';
  17. echo '<option value="">Select</option>';
  18.  
  19. while ($row = mysql_fetch_assoc($result))
  20. {
  21. $va = $row['username'];
  22. echo "<option value='$va'>$va</option>";
  23. }
  24. echo '</select>';
  25.  
  26. mysql_close($con);
  27. ?>
  28.  
  29. </body>
  30. </html>
  31.  
[Please use CODE tags when posting source code. Thanks! --pbmods]

hope it would help you.
May 23 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.