2

i have been trying to populate a dropdown menu with the columns of a SQL table, but i am stuck. Or all the fields in the dropdown menu are : Array, or the dropdown menu is populated with empty values.

am i missing something?

i tried this code.

SHOW COLUMNS, DESCRIBE 'TABLE' AND SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE = ikbb;

    <?php
    $conn = new mysqli('localhost', 'heijsdb_user', 'maus', 'heijsdb') 
    or die ('Cannot connect to db');
    $result = $conn->query("SHOW COLUMNS FROM ikbb FROM heijsdb");
    echo "selecteer een input om aan te passen";
    echo "<html>";
    echo "<body>";
    echo "<select name='id'>";
    while ($row = $result->fetch_assoc()) {

                $rows[0] = $row;
                  echo '<option value="'.$row.'">'.$row.'</option>';

}

    echo "</select>";
    echo "</body>";
    echo "</html>";
?>

result result 2

16
  • Possible duplicate of Get all columns from all MySQL tables Commented Jul 4, 2017 at 10:31
  • $row is an array. Commented Jul 4, 2017 at 10:31
  • value like $row[0] or $row[value] to set option value Commented Jul 4, 2017 at 10:32
  • 2
    how is this a duplicate since i want to populate a dropdown menu Commented Jul 4, 2017 at 10:32
  • I agree Maurice, that duplicate link is bad. Commented Jul 4, 2017 at 10:33

1 Answer 1

1

You're trying to populate a dropdown with an array, and this is why you see Array in your select input. Furthermore, you shouldn't add the html tag later than echo something.

I would recommend this query for getting the columns of a table :

SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='your_database' AND `TABLE_NAME`='your_table'

To populate a dropdown with these names, simply use this script :

<?php
$conn = new mysqli("server", "user", "password", "database");
$result = $conn->query("SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='your_database' AND `TABLE_NAME`='your_table'");
echo '<html>';
echo '<body>';
echo '<select name="id">';
while($row = $result->fetch_assoc()) {
    echo '<option value="'.$row['COLUMN_NAME'].'">'.$row['COLUMN_NAME'].'</option>';
}
echo '</select>';
echo '</body>';
echo '</html>';
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.