Jump to content

Populate listbox with database table names


stvalentine84

Recommended Posts

This code populates my listbox with the databases names that are on mySQL. However I am getting stuck when I try and make this poplulate my table names and not my database names. I was thinking about using

 

mysql_select_db($_POST['Select_DB']);

 

to select the database first then run through my script to grab the names but when i try and modify for table names I always get an empty listbox.

 

anyhelp is appreciated, Thanks!!

 

<html>
<head><title>List DB</title></head>
<body>
<form method="POST" action="sub.php"><div><center><p>Select DataBase
<input type="hidden" name="db_name" value="DB_names">
<SELECT NAME="Select_DB">
<?php

$conn = @mysql_connect( 'localhost', 'user', 'pass' )
or die( mysql_errno().': '.mysql_error());
$result = mysql_list_dbs( $conn );

while( $row = mysql_fetch_object( $result ) ):
echo "<option value=\"$row->Database\">$row->Database</option>";
endwhile;

// Free resources / close MySQL Connection
mysql_free_result( $result );
mysql_close( $conn );

?>
</select>
<input type="submit" value="Submit"></center></div>
</form>
</body>
</html>

I believe my error is somewhere around my while loop

 

 

<html>
<head><title>List DB</title></head>
<body>
<form method="POST" action="sub.php"><div><center><p>Select Tables
<input type="hidden" name="table_name" value="TABLE_names">
<SELECT NAME="Select_TABLE">

while( $row = mysql_fetch_object( $result ) ):
echo "<option value=\"$row->Database\">$row->Database</option>";
endwhile;

<?php

$conn = @mysql_connect( 'localhost', 'user', 'pass' )
or die( mysql_errno().': '.mysql_error());

#Selects the database from the DB Listbox and uses this to list the tables
$result = mysql_list_tables( $_POST['Select_DB'] );

while( $row = mysql_fetch_object( $result ) ):
echo "<option value=\"$row->Database\">$row->Database</option>";
endwhile;

// Free resources / close MySQL Connection
mysql_free_result( $result );
mysql_close( $conn );

?>
</select>
<input type="submit" value="Submit"></center></div>
</form>
</body>
</html>

try changing the code of your while statement to one of the following...

 

while($row = mysql_fetch_object($result))
{
      echo "<option value=\"$row->Database\">$row->Database</option>";
}

or

while($row = mysql_fetch_array($result))
{
      echo "<option value=\"$row->Database\">$row->Database</option>";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.