pobman Posted March 15, 2010 Share Posted March 15, 2010 Hi, I have just started learning php and mysql, and I'd like to make a dropdown box from a table. I have tried several postings but I can't get them to work so made my own thread. I am getting a blank page so all is not well. so far I have: <form class="table" action="check.php" method="post"> <div class="inner-form"> <div class="msg msg-info"> <p>Please select which domain you wish to edit.</p> </div> <? mysql_connect(localhost, user, pass); @mysql_select_db(servers_db) or die( "Unable to select database"); echo "<select name=form>n<option selected>choose Server</option>"; $query = "select Name, from CES"; while($row = mysql_query($query)) { echo "<option value="" . $row[server] . "">" . $row[NAME] . "</option>" ; } ?></div> </select> <input type="submit" VALUE="Edit"> </form> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/195380-how-to-make-a-dropdown-box-work/ Share on other sites More sharing options...
schilly Posted March 15, 2010 Share Posted March 15, 2010 mysql_query returns a results set. from that you need to get the rows from the result set. $query = "select Name, from CES"; $results = mysql_query($query); while($row = mysql_fetch_array($results)) { echo "<option value=\"" . $row[server] . "\">" . $row[NAME] . "</option>"; } Quote Link to comment https://forums.phpfreaks.com/topic/195380-how-to-make-a-dropdown-box-work/#findComment-1026704 Share on other sites More sharing options...
pobman Posted March 16, 2010 Author Share Posted March 16, 2010 thanks for your reply, I get the dropdown box now but no entries. $query = 'SELECT `Name` FROM `CES` WHERE `Name` IS NOT NULL'; $results = mysql_query($query); while($row = mysql_fetch_array($results)) { echo "<option value=\"" . $row[server] . "\">" . $row[NAME] . "</option>"; Is the code correct for displaying the results in the dropdown box? Quote Link to comment https://forums.phpfreaks.com/topic/195380-how-to-make-a-dropdown-box-work/#findComment-1026716 Share on other sites More sharing options...
LeadingWebDev Posted March 16, 2010 Share Posted March 16, 2010 correct will be like that $query=mysql_query("SELECT * FROM `CES` WHERE `Name` IS NOT NULL"); while($row = mysql_fetch_array($query)) { echo "<option value=\"$row[server]\">$row[Name]</option>"; pay attention, you were chosen only Name from DB, where from you getting server? second thing SELECT * works faster that SELECT `row`, `row` etc... 3rd, if you post name like ".$row[name]." its should be ".$row['name']." with quotes around index. you can also use inside double quotes ("...") as echo "<option value= \"$row[server]\"> $row[NAME]</option>"; and so you won't quote index inside []. also row name must be exactly as inside db, if your column named NAME it should be $row[NAME], if name $row[name], etc... else you will have blank result and notice undefined index. sorry for soo bad english Quote Link to comment https://forums.phpfreaks.com/topic/195380-how-to-make-a-dropdown-box-work/#findComment-1026718 Share on other sites More sharing options...
pobman Posted March 16, 2010 Author Share Posted March 16, 2010 echo "<option value=\"$row[server]\">$row[Name]</option>"; Thanks for your help it is working now, but you raised a question that had me thinking. I am unsure if the line above is correct even though it does work. The db = server_db table = CES field = Name These are the only varibles in my script, server was there as it was in a posting I read and I used the copy and paste without really understanding the code being used. How should the code be with out the Server varible? Quote Link to comment https://forums.phpfreaks.com/topic/195380-how-to-make-a-dropdown-box-work/#findComment-1026764 Share on other sites More sharing options...
LeadingWebDev Posted March 16, 2010 Share Posted March 16, 2010 What really you need to give as Server? $row[server] means you get this variable from database table CES row Server, if u dont have this row in your table its being blank var and return notices of not existed index. please quick, g2g sleep, my time almost 5AM Quote Link to comment https://forums.phpfreaks.com/topic/195380-how-to-make-a-dropdown-box-work/#findComment-1026768 Share on other sites More sharing options...
dreamwest Posted March 16, 2010 Share Posted March 16, 2010 $results = mysql_query("SELECT `Name` FROM `CES` WHERE `Name` !='' "); while($row = mysql_fetch_assoc($results)) { echo "<option value='{$row['Server']}'> {$row['NAME']}</option>"; } Quote Link to comment https://forums.phpfreaks.com/topic/195380-how-to-make-a-dropdown-box-work/#findComment-1026769 Share on other sites More sharing options...
gizmola Posted March 16, 2010 Share Posted March 16, 2010 second thing SELECT * works faster that SELECT `row`, `row` etc... sorry for soo bad english While I appreciate the language barrier, this is fundamentally bad advice. SELECT * does not work faster --- and in many cases it can bring back a lot of data that is not needed or required, which is in no way faster. The best practice is to provide a list of the COLUMNS required. Please also take note that a COLUMN is not the same as a ROW. Since result sets are comprised of Rows and Columns, this is an important distinction when you're talking about relational databases. A query returns a result SET which may contain 0 -> Size of all tables combined ROWS. All rows in the result set have the same number of columns (or fields). Each ROW in the result set needs to be FETCHED from the server. Quote Link to comment https://forums.phpfreaks.com/topic/195380-how-to-make-a-dropdown-box-work/#findComment-1026777 Share on other sites More sharing options...
gizmola Posted March 16, 2010 Share Posted March 16, 2010 $results = mysql_query("SELECT `Name` FROM `CES` WHERE `Name` !='' "); while($row = mysql_fetch_assoc($results)) { echo " {$row['NAME']}"; } This code certainly can not work, because you do not specify that the query return a column named 'Server' in your query. This might help -- without knowing the structure of your CES table, it's not possible to say for sure. $results = mysql_query("SELECT `Name`, `Server` FROM `CES` WHERE `Name` !='' "); Quote Link to comment https://forums.phpfreaks.com/topic/195380-how-to-make-a-dropdown-box-work/#findComment-1026779 Share on other sites More sharing options...
dreamwest Posted March 16, 2010 Share Posted March 16, 2010 $results = mysql_query("SELECT `Name` FROM `CES` WHERE `Name` !='' "); while($row = mysql_fetch_assoc($results)) { echo "<option value='{$row['Server']}'> {$row['NAME']}</option>"; } This code certainly can not work, because you do not specify that the query return a column named 'Server' in your query. This might help -- without knowing the structure of your CES table, it's not possible to say for sure. $results = mysql_query("SELECT `Name`, `Server` FROM `CES` WHERE `Name` !='' "); My bad. Im doing 10 things at once ATM. I would add SELECT * if you queries are below 10,000 requests per day Quote Link to comment https://forums.phpfreaks.com/topic/195380-how-to-make-a-dropdown-box-work/#findComment-1026786 Share on other sites More sharing options...
pobman Posted March 17, 2010 Author Share Posted March 17, 2010 Thanks all for your advice I really appreciated your responses. Good news is it is working for me so lesson 1 has now been learnt. Next thing is choosing a result from the drop down and using it in a new query, time for a new post! Quote Link to comment https://forums.phpfreaks.com/topic/195380-how-to-make-a-dropdown-box-work/#findComment-1027789 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.