Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/195380-how-to-make-a-dropdown-box-work/
Share on other sites

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>";
}

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?

 

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 ^_^

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?

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

 

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.

$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` !='' ");

$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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.