Jump to content

List all users in a database in a drop-down menu


Willpool
Go to solution Solved by Barand,

Recommended Posts

Hi.

 

Sorry if I've posted in the wrong forum or something - I joined this forum a few seconds ago. :P

 

I'm trying to create something which displays all of the users in a database table in a drop-down menu. I've tried this code:

<?php

include 'config.php';
session_start();
$suser       = $_SESSION['user'];
$con          =  mysql_connect($config['mysql_host'], $config['mysql_user'], $config['mysql_pass']);
mysql_select_db($config['database']);
$cpuTable = $config['cpuTable'];
$query       = mysql_query("SELECT * FROM $cpuTable WHERE suser='$suser'");

while($row = mysql_fetch_assoc($qa)) {
     $users = $row['username'];
     echo "
              <select name='cpuser'>
              <option value='user'>$users</option>
              </select>";
}

I created two users in the database - "cp_test1" and "cp_test2", tried the code and it outputted this:

 

2w3chht.png

Instead of listing the users in one drop-down menu, it creates two and lists one in each. Can anyone help?

 

Thanks

 

 

Link to comment
Share on other sites

Changed it to:

while($row = mysql_fetch_assoc($query)) {
$users = $row['username'];
}

echo "
<select name='cpuser'>
<option value='$users'>$users</option>
</select>
";

It now outputs one drop-down menu but it only displays one of the users.

Link to comment
Share on other sites

 

Not surprising, you moved it all outside the loop

echo "<select name='cpuser'>";

while($row = mysql_fetch_assoc($query)) {
    $users = $row['username'];
    echo "<option value='$users'>$users</option>"
}


echo "</select>";

 

Thanks, it works now.

Link to comment
Share on other sites

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.