Php 如何在下拉列表中列出数据库的所有表(而不是记录)

Php 如何在下拉列表中列出数据库的所有表(而不是记录),php,mysql,sql,database,Php,Mysql,Sql,Database,我正在创建一个onlineshop,用户将通过键入标题、说明、价格和图像来添加一条新记录,但我还想列出我的所有表名的下拉列表,供用户选择它们作为添加产品的类别。 我的数据库的详细信息如下: db name=在线商店 connect.php <?php // Try to connect to MySQL $connect = mysql_connect('localhost','root', '') or die('Sorry could not connect t

我正在创建一个onlineshop,用户将通过键入标题、说明、价格和图像来添加一条新记录,但我还想列出我的所有表名的下拉列表,供用户选择它们作为添加产品的类别。 我的数据库的详细信息如下: db name=在线商店

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

<?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;
}
?>

索引包含一个表单和一个我在网上找到的调用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.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>


标题: 描述: 价格: 上传你的图片
我得到的只是一个空的下拉列表

我对PHP非常陌生,所以请让我知道对我的数据库的任何建议,以及您是否建议我包含一个脚本

我很确定问题在于index.html中的getTablesList()函数没有返回任何内容


打开任何建议

而不是尝试运行“显示表格”,尝试

SELECT *
FROM information_schema.tables
WHERE table_schema = 'onlineshop';

您需要在WHERE子句中使用更多子句来限制显示的实际值表。

如果您是PHP5.5.0之前的版本,可以使用
mysql\u list\u tables
函数(Docs)


如果您使用的是PHP5.5.0或更高版本,我不确定是否有类似的方法用于
mysqli
对象,但您也可以将查询从
“SHOW TABLES”
修改为
“SHOW TABLES from$db_name”

,这是一个关于如何从PHP.NET列出表的示例

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);
    ?>

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>


我们谈论的是很多桌子吗。?手动操作可能不是一个坏主意,以防将来您添加一个不想在列表中显示的表。您说的“手动”是什么意思?手动操作就像您自己将它们添加到下拉列表中一样。我会将它们与我的数据库表相匹配吗?是的。除非你一直在添加新的。当我同时使用这两种方法时,我的下拉列表中会出现一个“无数据”字段。有什么问题吗?无数据结果列在index.html getTableList()函数的语句中
rowCount
方法不一定返回您期望的结果();在某些数据库中,它会,但不是全部。尝试使用insteadis有没有办法修改index函数的php代码?我对它不是很满意,我只是想让它更简单一些,有更多处理PDO对象经验的人可以在这里更好地指导您,但似乎使用这些方法来获取结果集的大小存在问题,或者您正在查询的数据集存在问题。如果您可以验证是否正确创建了所有表,则表明您正在错误地获取结果集的大小。不幸的是,在这方面我无能为力;我个人从来没有遇到过这样的情况,我必须提前知道下拉列表中要使用的表单(HTML)?我现在可以看到表格,但在文本格式中,我需要将上面的php函数加载到HTML表单(下拉列表)中好的,等一下,让我告诉你另一种方法,它可以用你的代码完成上面的函数可以工作,我可以看到我列出的表。通过这种方式,我相信如果我创建一个带有下拉列表的表单来加载php文件,它就会工作。我只需要找出formNotice:Undefined变量:tables in…./dropdown.php中使用的循环,在第21行我得到了列表,但是下拉列表上的名称是{$row[0]}什么样的错误?这可能是最好的方法