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

1 Answers1

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>';
?>
DamiToma
  • 921
  • 3
  • 9
  • 27