473,396 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Dropdown Box using Database column

1
Hi guys;
I'm new here, was hoping some of you guys could help me out.

Basically I'm wanting to set up a page where a user can select from a drop down box a list of different data, however this data is all stored in a database. Is it possible so on page load or when clicking the dropdown box that the data is pulled from the database and can be an option for the user to select.
May 29 '09 #1
2 1500
Ciary
247 Expert 100+
you cant imagine how common that question is:

Expand|Select|Wrap|Line Numbers
  1. $link = mysql_connect("myserver","root","");
  2. mysql_select_db("mydb",$link);
  3.  
  4. $result = mysql_query("SELECT mycolumn FROM mytable",$link);
  5. $arr = mysql_fetch_assoc($result);
  6.  
  7. mysql_free_result($result);
  8. mysql_close($link);
then your select would be like this:
Expand|Select|Wrap|Line Numbers
  1. <select>
  2. <?php foreach($arr as $val){
  3. echo "<option id='option-".$val.">".$val."</option>\n";
  4. } ?>
  5. </select>
May 29 '09 #2
Atli
5,058 Expert 4TB
The mysql_fetch_assoc function only returns a single row, not the entire table.

It should be more like:
Expand|Select|Wrap|Line Numbers
  1. $link = mysql_connect("myserver","root","");
  2. mysql_select_db("mydb",$link);
  3.  
  4. $result = mysql_query("SELECT mycolumn FROM mytable",$link);
  5. if($result) {
  6.   echo '<select name="myselect">';
  7.   while($row = mysql_fetch_assoc($result)) {
  8.     echo "<option value=\"{$row['mycolumn']}\">{$row['mycolumn']}</option>";
  9.   }
  10.   echo "</select>";
  11. }
  12. else {
  13.   echo "<p>No data</p>";
  14. }
  15.  
  16. mysql_free_result($result);
  17. mysql_close($link);
  18.  
May 29 '09 #3

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

Similar topics

1
by: sk | last post by:
I have a situation where the list of tables(database) and the columns for each table must be displayed for selection. for each member(displayed in each row) i have to select a table and a column from...
3
by: John Blair | last post by:
Hi, I want to select an id column and a name column from the database and have the results databound to a SINGLE dropdown control with the value property matched to the database id column and...
2
by: Kreuzer Michael | last post by:
Hi all, I have a problem: i want a full table of a Access DB in a Dropdown list. In the database, there is one table with index, name, telephonenumber. I have the OleDB Adapter & Dataset, but...
3
by: Cagey | last post by:
What I'm trying for: If this selection or if click on selection (highlighted line choice/ which ever selection change) w/in query's combo dropdown list box (on Switchboard), then Open in...
4
by: AccessRookie | last post by:
Ok, I am pretty new to Access (Using Access 2002 at my job). Here is what I want to do. I want to create a database that has a form. On this form will be a dropdown menu (combobox). Depending...
5
by: =?Utf-8?B?bWNhdWxpZmZl?= | last post by:
I have an old application ( pre-VB5) that I need to add a select/option list to. This is an edit program so the values for the form will be retrieved from a database. How do I set the value of...
0
by: Andrus | last post by:
I'm using WinForms DataGridView I need to make dropdown list wider that grid column width. I tried the following code, but dropdown list widht is the same as column width. How to increase...
2
by: anonymoushamster | last post by:
Dropdown A is column from database Dropdown B is column from database Data in A and B must be from same row. If user click an item from A, then B is updated to display its data from...
1
by: Stephen D Cook | last post by:
I am trying to tie a column in a database to a dropdown list, but having very little luck. The table is bound to the .aspx page via oledbadapter1 and oleconnection1, but when I try to create a...
0
by: progman417 | last post by:
I have a gridview with a dropdown list in an EditItemTemplate. I haven't set a width for the columns, so that they dynamically size according to the data displayed. When it goes into edit...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.