Reputation: 871
.Hi everyone i need a little help for my dropdown menu. .I already know how to populate a dropdown list from a database but what i want to do is to fill my dropdown list with the list of databases available in my MySQL server. can anyone please help me with the proper coding for this one. Thanks in advance! by the way i am using php.
update:
.alright i tried this:
$sql = "SHOW databases";
$result = mysql_query($sql);
echo '<form method="post" id="try" action="pillar.php">';
echo '<select name="batch" id="batch">';
echo '<option>';
while($r = mysql_fetch_assoc($result))
{
$database = $r['Database'];
echo '<option>'.$database.'</option>';
}
echo '</select><br>';
echo '<input type="submit">';
echo '</form>';
IT works! but is there a way that i could exclude any of the retrieved values? because what i want to output on my dropdown is the list of batch which is the databases. but what happens is that the dropdown includes the default databases of MySQL. like "information_schema" and "mysql".
Upvotes: 0
Views: 1041
Reputation: 9299
With the proper DB rights within MySQL you can query the database with SHOW DATABASES
$ignore = array('information_schema', 'mysql', 'test');
$sql = "SHOW DATABASES";
$q = mysql_query($sql);
while($r = mysql_fetch_assoc($q)) {
$database = $r['Database'];
if(!in_array($database, $ignore)) {
// do something
}
}
Upvotes: 1
Reputation: 9671
while($r = mysql_fetch_assoc($result))
{
if($r['Database'] == 'information_schema'
|| $r['Database'] == 'test'
|| $r['Database'] == 'mysql' ){
}else{
$database = $r['Database'];
echo '<option>'.$database.'</option>';
}
}
(I don't want to many FALSE operators in a single if clause, hence the action in else)
Upvotes: 1