[英]how to list all TABLES(not records) of a database in a drop down list MySQL PHP
I am creating an onlineshop, where the user is going to add a new record, by typing the title, description, price and image, but I also want to put a dropdown list of all my TABLES names for the user to select them as a category for adding the product. 我正在创建一个网上商店,用户将通过键入标题,描述,价格和图像来添加新记录,但我也想放置一个我所有表名的下拉列表,以供用户选择添加产品的类别。 The details of my database are: db name = onlineshop
我的数据库的详细信息是:数据库名称= onlineshop
connect.php connect.php
<?php
// Try to connect to MySQL
$connect = mysql_connect('localhost','root', '') or die('Sorry could not connect to database');
// Check connect and return error if failed
$use_db = mysql_select_db('onlineshop');
$create_db = "CREATE DATABASE onlineshop";
if(!$use_db) {
echo mysql_error();
mysql_query($create_db);
mysql_select_db('onlineshop');
}
$con=mysqli_connect('localhost','root', '');
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Create database
$sql="CREATE DATABASE onlineshop";
if (mysqli_query($con,$sql))
{
echo "Database my_db created successfully";
}
else
{
echo "Error creating database: " . mysqli_error($con);
}
//main table
$sql = 'CREATE TABLE mens( '.
'id INT NOT NULL AUTO_INCREMENT, '.
'title VARCHAR(20) NOT NULL, '.
'description VARCHAR(45) NOT NULL, '.
'price FLOAT NOT NULL, '.
'image varchar(200),'.
'image_small varchar(200),'.
'primary key ( id ))';
//copy attributes of the main table
$sql2= 'CREATE TABLE women AS ( SELECT * FROM mens where 1=2)';
$sql3= 'CREATE TABLE kids AS ( SELECT * FROM mens where 1=2)';
$sql4= 'CREATE TABLE infants AS ( SELECT * FROM mens where 1=2)';
$sql5= 'CREATE TABLE baby_books AS ( SELECT * FROM mens where 1=2)';
$sql6= 'CREATE TABLE garden AS ( SELECT * FROM mens where 1=2)';
$sql7= 'CREATE TABLE comics AS ( SELECT * FROM mens where 1=2)';
$sql8= 'CREATE TABLE cooking AS ( SELECT * FROM mens where 1=2)';
$sql9= 'CREATE TABLE desktop AS ( SELECT * FROM mens where 1=2)';
$sql10= 'CREATE TABLE laptop AS ( SELECT * FROM mens where 1=2)';
$sql11= 'CREATE TABLE mobile AS ( SELECT * FROM mens where 1=2)';
$sql12= 'CREATE TABLE misc AS ( SELECT * FROM mens where 1=2)';
$sql13= 'CREATE TABLE moviestv AS ( SELECT * FROM mens where 1=2)';
$sql14= 'CREATE TABLE music AS ( SELECT * FROM mens where 1=2)';
$sql15= 'CREATE TABLE games AS ( SELECT * FROM mens where 1=2)';
$retval = mysql_query( $sql, $connect );
$retval2 = mysql_query($sql2, $connect);
$retval3 = mysql_query($sql3, $connect);
$retval4 = mysql_query($sql4, $connect);
$retval5 = mysql_query($sql5, $connect);
$retval6 = mysql_query($sql6, $connect);
$retval7 = mysql_query($sql7, $connect);
$retval8 = mysql_query($sql8, $connect);
$retval9 = mysql_query($sql9, $connect);
$retval10 = mysql_query($sql10, $connect);
$retval11 = mysql_query($sql11, $connect);
$retval12 = mysql_query($sql12, $connect);
$retval13 = mysql_query($sql13, $connect);
$retval14 = mysql_query($sql14, $connect);
$retval15 = mysql_query($sql15, $connect);
?>
dropdown.php dropdown.php
<?php
$connect = mysql_connect('localhost','root', '') or die('Sorry could not connect to database');
function runSQL($sql)
{
$mysqlConnection = getConnection();
$ResultSet = $mysqlConnection->query($sql);
return $ResultSet;
}
function getTableList()
{
$sql = "SHOW TABLES";
$ResultSet = runSQL($sql);
if(!$ResultSet)
{
echo "Table list not found";
}
return $ResultSet;
}
?>
index contains a form as well as a function that i found online that calls the dropdown.php 索引包含一个表单以及一个我在网上找到的调用dropdown.php的函数
index.html 的index.html
<form action="insert.php" method="post">
<br>
<div><label for="title">Title: </label><input type="text" name="title"/></div>
<div><label for="description">Desc: </label><input type="text" name="description"/></div>
<div><label for="price">Price: </label><input type="text" name="price" /></div>
<input type="submit" name="submit" value="Submit">
</form>
<form id="imageform" method="post" enctype="multipart/form-data" action='ajaximage.php'>
Upload your image <input type="file" name="photoimg" id="photoimg" />
</form>
<div id='preview'>
</div>
<?php
include_once 'dropdown.php';
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<select name="Tables" id="ddTables">
<?php
$tableResults = getTableList();
if($tableResults)
{
if($tableResults->rowCount() > 0)
{
$tables = $tableResults->fetchAll(PDO::FETCH_NUM);
foreach($tables as $table)
{
$name = $table[0];
echo '<option value="'.$name.'">'.$name.'</option>';
}
}
}
else
{
echo '<option value="0">No Data</option>';
}
?>
</select>
<input type="submit" id="tableSubmit" value="Submit"/>
</form>
All i get is an empty dropdown list 我所得到的只是一个空的下拉列表
I really new into PHP, so please let me know of ANY suggestions for my database, as well as if you suggest me to include a script. 我真的是PHP的新手,所以请告诉我有关数据库的任何建议,以及如果您建议我包括一个脚本。
i pretty sure that the problem is that the getTablesList() function in the index.html is not returning anything. 我很确定问题是在index.html中的getTablesList()函数没有返回任何东西。
open to any suggestions 接受任何建议
Instead of trying to run `SHOW TABLES', try 与其尝试运行“ SHOW TABLES”,不如尝试
SELECT *
FROM information_schema.tables
WHERE table_schema = 'onlineshop';
You are going to want to use more clauses within the WHERE clause to limit the actuals tables that are shown. 您将要在WHERE子句中使用更多子句来限制显示的实际表。
If you're pre-PHP 5.5.0, you can use the mysql_list_tables
function (Docs http://us3.php.net/manual/en/function.mysql-list-tables.php ) 如果您使用的是PHP 5.5.0之前的版本,则可以使用
mysql_list_tables
函数(文档http://us3.php.net/manual/en/function.mysql-list-tables.php )
If you're using PHP 5.5.0 or later, I'm not sure if there's a similar method for mysqli
objects or not, but you can also modify your query from "SHOW TABLES"
to "SHOW TABLES FROM $db_name"
如果您使用的是PHP 5.5.0或更高版本,则不确定是否有针对
mysqli
对象的类似方法,但是您也可以将查询从"SHOW TABLES"
修改为"SHOW TABLES FROM $db_name"
This is an example on how to list Tables from PHP.NET 这是有关如何从PHP.NET列出表的示例
http://us3.php.net/manual/en/function.mysql-list-tables.php http://us3.php.net/manual/en/function.mysql-list-tables.php
dropdown.php dropdown.php
<?php
$dbname = 'mysql_dbname';
if (!mysql_connect('mysql_host', 'mysql_user', 'mysql_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)) {
$tables .='<option value="{$row[0]}">{$row[0]}</option>';
}
mysql_free_result($result);
?>
in index.html 在index.html中
<?php
include_once 'dropdown.php';
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<select name="Tables" id="ddTables">
<?php
echo $tables;
?>
</select>
<input type="submit" id="tableSubmit" value="Submit"/>
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.