[英]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 要获取数据库: 从SQL Server获取数据库列表
To get tables in a database : http://php.net/manual/en/function.mysql-list-tables.php 要获取数据库中的表: http : //php.net/manual/zh/function.mysql-list-tables.php
Use AJAX to change the tables based on database drop down box. 使用AJAX根据数据库下拉框更改表。
Using PDO, you would just use the SHOW TABLES
command: 使用PDO,您只需使用SHOW TABLES
命令:
$result = $db->query("SHOW TABLES");
To get a list of all databases, use the SHOW DATABASES
command: 要获取所有数据库的列表,请使用SHOW DATABASES
命令:
$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, 创建两个PHP文件,如下所示:
(1). (1)。 index.php : 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). (2)。 fetch_data.php : 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. 我认为这可以帮助您解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.