0

We have been given the task where we have to populate a drop down box with data from a database using PHP and MySQL etc. This is the code I have so far. So far since I have tested it, it is showing the drop down box but other than that there is nothing in the drop down menu populating it.

<?php

$hostname = 'host';
$username = 'username';
$password = 'password';
$databaseName = 'dbname';

$connect = mysqli_connect($hostname, $username, $password, $databaseName);

$query = 'SELECT * FROM `User`';

$result1 = mysqli_query($connect, $query);
$result2 = mysqli_query($connect, $query);

$options = '';
while ($row2 = mysqli_fetch_array($result2)) {
    $options = $options . "<option>$row2[1]</option>";
}

?>
<!DOCTYPE html>
<html>
<head>
    <title> PHP SELECT OPTIONS FROM DATABASE </title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<select>
    <?php while ($row1 = mysqli_fetch_array($result1)):; ?>
        <option value="<?php echo $row1[0]; ?>"><?php echo $row1[1]; ?>        </option>
    <?php endwhile; ?>
</select>
<select>
    <?php echo $options; ?>
</select>
</body>
</html>
luchaninov
  • 6,792
  • 6
  • 60
  • 75
Rex Richards
  • 5
  • 1
  • 4
  • there's that familiar `while` loop again *lol* oh you're so funny. New profile huh? You need to stop trolling buddy, you've worn out the fun from the first 2. – Funk Forty Niner May 17 '16 at 21:00
  • @Fred-ii- Since I'm completely out of the loop (pun intended), was he spamming or something? – Mike May 17 '16 at 21:01
  • @Mike Yeah, 2 deleted questions/profiles, same `while` loop with the semi-colon at the end. As if that loop works. This in the past 2-3 days now. – Funk Forty Niner May 17 '16 at 21:02
  • @Fred-ii- I don't see why it wouldn't work... Extra semicolons don't do anything: https://3v4l.org/2eTBU – Mike May 17 '16 at 21:08
  • @Mike semi-colon's an end of statement. I can't see how it would work in mysql. Sure your eval demo works, but this may not be the same when pulling data in from a db. – Funk Forty Niner May 17 '16 at 21:09
  • @Fred-ii- This is literally the first time I've made an account on this website so I don't know what you are talking about really – Rex Richards May 17 '16 at 21:12
  • @Fred-ii- If there were a syntax error here the script wouldn't even execute at all. I'm leaning more towards either the `User` table not existing or it being empty. – Mike May 17 '16 at 21:13
  • this is just far too coincidental. Either that, or you guys are all pulling the same bad example from the same place. I could be wrong, but like I said; this is far too coincidental. funny how a newbie account knows how to ping somebody though – Funk Forty Niner May 17 '16 at 21:14
  • Why are you fetching twice? – Jay Blanchard May 17 '16 at 21:20
  • 1
    Possible duplicate of [Fetching data from MySQL database to html dropdown list](http://stackoverflow.com/questions/10009464/fetching-data-from-mysql-database-to-html-dropdown-list) – miken32 May 17 '16 at 22:05

1 Answers1

0

Try

  While($row = mysqli_fetch_assoc($result)){

 $therow = row['column_name'];

  echo '
  <option value='.$therow.'>'.$therow.'</option>
  ';
  }

Hope this give you a clue Cos in your code above, you had two results and it's really hard to tell which is which and what does what.

Also, using the associative array makes it easier or simple to work with the columns in the table so one doesn't get confused...

Alternatively, if you need the options as a variable to use outside the while loop.

Do

  $option .= '<option value='.$therow.'>'.$therow.'</option>';

Inside the loop

Then in your html

   <select>

      <?php echo $option; ?>

    </select>
Mueyiwa Moses Ikomi
  • 1,069
  • 2
  • 12
  • 26
  • 3
    Why should the OP "try this"? A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard May 17 '16 at 21:13
  • @JayBlanchard I was about to say the same thing. – Mike May 17 '16 at 21:13
  • Even with your edit to explain your answer, this code does exactly the same as what's in the question. It may look prettier, but it doesn't solve the problem. – Mike May 17 '16 at 21:28
  • fink the problem is that it's not showing the options in the dropdown – Mueyiwa Moses Ikomi May 17 '16 at 21:31