Jump to content

Recommended Posts

Hi,

 

I'm trying to fill a select box with mysql results from a query and have come up with the following code:

<select name="userlevel">
	<?php 
		$query = "SELECT userlevel FROM user_levels ORDER BY userlevel_id";
		$result = mysql_query($query);
		while ($row = mysql_fetch_array($result)){
			echo "<option value= .$row .> </option>";
		}
		echo "</select>";
	?>

 

So far the select box shows empty, and I do have at least one field in the table.

Also if there is a way to do this with oop, please let me know.

Thanks for your help.

Link to comment
https://forums.phpfreaks.com/topic/134418-solved-fill-select-box-with-query/
Share on other sites

<select name="userlevel">
	<?php 
		$query = "SELECT * FROM user_levels ORDER BY userlevel_id";
		$result = mysql_query($query);
		while ($row = mysql_fetch_assoc($result)){
			echo "<option value=\"$row[userlevel]\"> </option>";
		}
		echo "</select>";
	?>

 

try that

Do you mean it is empty with no drop down options or are there drop down options just empty ones.

 

anyway here is a code fix

<select name="userlevel">
      <?php 
         $query = "SELECT userlevel FROM user_levels ORDER BY userlevel_id DESC";
         $result = mysql_query($query);
         while ($row = mysql_fetch_array($result))
         {
            echo "<option value= \"".$row['userlevel']."\">".$row['userlevel']."</option>";
         }
         echo "</select>";
      ?>

For an optimized query you should really only SELECT the fields you need like above, not *.

@Maq, depends on the size of the table and the amount of rows it has.

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.