zerey
zerey

Reputation: 871

populate dropdown with table list

.hey guys how can i populate a dropdown list with the list of tables from a certain database?

$db = mysql_select_db('thepillar');
$sql = "SHOW TABLES";
$result = mysql_query($sql);
echo '<form method="post" id="try" action="pillar.php">';
echo 'Select Batch: ';
echo '<select name="batch" id="batch">';
echo '<option>';
while($r = mysql_fetch_assoc($result)) 
{
    $tables = $r;
echo '<option>'.$tables.'</option>';
}

.i have tried the code above but the dropdown list is only filled with the word "Array" multiple times depending on how many tables are there in the database.

.help pls!

Upvotes: 1

Views: 1052

Answers (3)

JYelton
JYelton

Reputation: 36512

Your $tables variable is an associative array. You need to specify which index of the array you want to output between the <option> tags.

echo '<option>'.$tables['TABLE_NAME'].'</option>';

See the print_r output for what the index name is.

Upvotes: 0

Lawrence Cherone
Lawrence Cherone

Reputation: 46602

replace

$tables = $r;

with

$tables = $r['Tables_in_thepillar'];

also you got an extra echo '<option>';

above the loop

Upvotes: 0

Mike Lewis
Mike Lewis

Reputation: 64137

while($r = mysql_fetch_array($result))
{
    echo $r[0]."<br />";
}

Upvotes: 1

Related Questions