简体   繁体   中英

Fetching all tables a database

I have made two drop down boxes. The first drop lists the available databases. When a user selects a database from the first drop down box, I would like to populate the 2nd drop down box with all tables in that that database.

Can anyone suggest how I might do this?

This is my code which I have tried

School Name:<select style="width: 140px" name="users" >
                    <option value="">Select school_name </option>

<?php
$link = mysql_connect('localhost', 'root', '');
$db_list = mysql_list_dbs($link);

while ($row = mysql_fetch_object($db_list)) {

?>
<option value='<?php echo $row->Database; ?>'><?php echo $row->Database; ?>   </option>
    <?php }?>

</select><br/>
Class:<select style="width: 140px" name="users" >
                    <option value="">Select school_name </option>

    <?php
    $link = mysql_connect('localhost', 'root', '');
    $db_list = mysql_list_tables($link);                            
    while ($row = mysql_fetch_object($db_list)) {

    ?>
    <option value='<?php echo $row->Tables; ?>'><?php   echo $row->Tables; ?></option>
              <?php }?>

</select>

<?php
}
?>

Now I want to get all the tables in second drop down based on first drop down box.

To get databse : Get list of databases from SQL Server

To get tables in a database : http://php.net/manual/en/function.mysql-list-tables.php

Use AJAX to change the tables based on database drop down box.

Using PDO, you would just use the SHOW TABLES command:

$result = $db->query("SHOW TABLES");

To get a list of all databases, use the SHOW DATABASES command:

$result = $db->query( 'SHOW DATABASES' );

Try this:

To fetch the DB lising:

SELECT SCHEMA_NAME FROM information_schema.SCHEMATA ;

To fetch tbe Table Listing:

SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'DBName';

Just try with the following Sample :

Create Two PHP files as follows,

(1). index.php :

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
function get_tables(db_name){
$.post("fetch_data.php", { db_name: db_name },
function(data) {
$("#tables_list").html(data);
}
);
}
</script>
<table>
<tr><td><label>Select Your Database Name :</label></td><td><select name="database_list" onChange="get_tables(this.value);">
<option value="Wp-Sparks">Wp-Sparks</option>
<option value="Ja_Sparks">Ja_Sparks</option>
<option value="test">Test</option>
</select></td></tr>
<tr><td><label>Select Your Database Tables :</label></td><td><select name="tables_list" id="tables_list"></select></td></tr>
</table>

(2). fetch_data.php :

<?php
$dbname =  mysql_real_escape_string($_POST['db_name']);
if (!mysql_connect('HOST_NAME', 'USER_NAME', 'PASSWORD')) {
    echo 'Could not connect to mysql';
    exit;
}
$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);

if (!$result) {
    echo "DB Error, could not list tables\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}
while ($row = mysql_fetch_row($result)) {
?>
<option value="<?php echo $row[0];?>"><?php echo $row[0];?></option>
<?php
}
mysql_free_result($result);
?>

I think this may help you to resolve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM