1

I have been trying to populate a drop down menu by pulling the needed information from a database through php and mysql however I haven't been able to achieve this and so far when I run it I just get a drop down box with nothing inside of it. Any help on this would be great as I am really stuck as to where I am going wrong.

<select id="dung-name-select" name="dung-select">
    <option name="" disabled selected hidden>Name</option>
    <option value="*">All</option>
        <?php 
        //Database Query
        $query = "SELECT Name FROM dungeon";
        $result = mysqli_query($connection, $query);
        if (!$result) {
            die("Database query failed.");
        }
            var_dump($result);
            while($row = mysqli_fetch_assoc($result)) {
        ?>
            <option value="<?= $row['Name']; ?>"><?= $row['Name']; ?></option>;
        <?php
        }
        ?>
</select>

I have connected to the database fine and can pull information down from it to populate a table for example however the drop down menu just isn't working.

NB: I need the value to be set to the name that I am also populating the drop down menu with.

Kai Jones
  • 145
  • 4
  • 14
  • aren't you just missing a space? `` => `` OR short tags `= $row['Name']?>`works too if configured. Also http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display?rq=1 might be usefull. also http://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-in-different-environments – Louis Loudog Trottier May 20 '17 at 02:19
  • I have read through 10+ other posts about it and tried the methods there and have included the space as well as adding `;` and still have the same result. – Kai Jones May 20 '17 at 02:27
  • 1
    Okay - add some error-checking. Do a `var_dump($result);`before the `while()`-loop, to see if `$result` even contain anything. – junkfoodjunkie May 20 '17 at 02:28
  • Sorry I am very new to php as you can probably tell! I have updated the code with your comment @junkfoodjunkie and still have the same result. Where would I see the var_dump as on my screen nothing appears to change. – Kai Jones May 20 '17 at 02:30
  • Check the HTML - not what you see. Or move the query and the fetching before the ` – junkfoodjunkie May 20 '17 at 02:31
  • your code works for me! – Adnan May 20 '17 at 02:35
  • I don't get why it isn't for me then... – Kai Jones May 20 '17 at 02:42
  • 1
    One thing to keep in mind is you can organize your code better by making direct calls and stripping out throw-away variables like `$query`. For example: `$connection->query("SELECT ...")` is less likely to have a mistake than if you define another variable that you might not spell correctly, or recycle somewhere else by accident. – tadman May 20 '17 at 02:43
  • Can you move your php code outside the select statement and see if var_dump($result); is displaying anython on your page? – MalcolmInTheCenter May 20 '17 at 02:44

1 Answers1

0

your code works for me, i tried something different hope it works for you.

<select id="dung-name-select" name="dung-select">
    <option name="" disabled selected hidden>Name</option>
    <option value="*">All</option>
     <?php 
        $result = $connection->query("SELECT Name FROM dungeon");
        if ($result) {
            while($row = $result->fetch_assoc()) {
        ?>
            <option value="<?= $row['Name']; ?>"> <?= $row['Name']; ?></option>;
    <?php
                }
        }       
        else {
            echo "No dungeons found"; 
            }
        ?>
</select>
Adnan
  • 101
  • 2
  • 10