简体   繁体   English

如何使用 PHP 将不同的用户选择的 SQL 表显示为 html 表?

[英]How do I get different user selected SQL tables displayed as an html table using PHP?

I am creating a page that prompts for server login info, and redirects after submitting correct information.我正在创建一个页面,提示输入服务器登录信息,并在提交正确信息后重定向。 Once on the next page, there is a select form with different table names representing tables inside of the database.进入下一页后,有一个选择表单,其中有不同的表名代表数据库中的表。 Once one is selected, and 'select table' is clicked, it is to display the entire table from the database as a table in html by using PHP.一旦选择了一个,然后单击“选择表”,就是使用PHP将数据库中的整个表显示为html中的表。 I'm not sure how to figure out the number of columns in a sql table to be able to display it using a loop and html table tags.我不确定如何确定 sql 表中的列数,以便能够使用循环和 html 表标签显示它。

<?
session_start();
if(!isset($_SESSION['session']))
{
    $_SESSION['session'] = 0;
}
if(isset($_POST['host']))
{
    $_SESSION['host'] = $_POST['host'];
    $_SESSION['dbname'] = $_POST['dbname'];
    $_SESSION['username'] = $_POST['username'];
    $_SESSION['pw'] = $_POST['pw'];
}

?>
<!DOCTYPE html>
<html>

<body>
    <?          
    if(isset($_POST['logout']))
    {
        $_SESSION['session'] = 0;
    }

    if(isset($_POST['submit']))
    {
        try
        {
            $db = new PDO("mysql:host=".$_POST['host'].";dbname=".$_POST['dbname'], $_POST['username'], $_POST['pw']);
        }
        catch(Exception $error)
        {
            die("Connection to user database failed: " . $error->getMessage());
        }

        try
        {
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO:: ERRMODE_EXCEPTION);
            $query = "SHOW TABLES";
            $results = $db->query($query)->fetchall();
            $_SESSION['session']=1;
        }
        catch(Exception $error)
        {
            echo "Problem with ~query, go back to home screen";
            $_SESSION['session']=0;
        }
    }       

    if($_SESSION['session'] == 0)
    {?>
    <form action="<?$_SERVER['PHP_SELF']?>" method="post" name='initialentry'>
        <table border='0' style='text-align: center'>
            <tr>
                <td style='text-align: right;'>Enter host name:</td>
                <td style='text-align: left;'>
                    <input type='text' name='host' value='localhost'>
                </td>
            </tr>
            <tr>
                <td style='text-align: right;'>Enter database name:</td>
                <td style='text-align: left;'>
                    <input type='text' name='dbname' value='zxyx999'>
                </td>
            </tr>
            <tr>
                <td style='text-align: right;'>Enter user name:</td>
                <td style='text-align: left;'>
                    <input type='text' name='username' value='zxyx999'>
                </td>
            </tr>
            <tr>
                <td style='text-align: right;'>Enter password:</td>
                <td style='text-align: left;'>
                    <input type='password' name='pw' width='15' value='12345'>
                </td>
            </tr>
            <tr>
                <td style='text-align: right;'>
                    <input type="reset" value="Reset">
                </td>
                <td style='text-align: left;'>
                    <input name='submit' type="submit" value="Submit">
                </td>
            </tr>
        </table>
    </form>
    <?}

    if($_SESSION['session']==1)
    {
        ?><form action='<?$_SERVER['PHP_SELF']?>' method='post' name='tableList'>
        <select name='selected'><?
            foreach($results as $row)
            {
                echo "<option value=".$row[0].">" . $row[0] . "</option>";
            }?>
        </select>
        <input type="submit" name="selectTable" value="Select Table">
        <input type="submit" name="logout" value="logout">
        </form><?
    }

    if(isset($_POST['selectTable']))
    {
        try
        {
            $db = new PDO("mysql:host=".$_SESSION['host'].";dbname=".$_SESSION['dbname'], $_SESSION['username'], $_SESSION['pw']);
        }
        catch(Exception $error)
        {
            die("Connection to user database failed: " . $error->getMessage());
        }

        try
        {
            $statement = $db->prepare("SELECT * FROM ".$_POST['selected']);
            $statement->execute();
        }
        catch(Exception $error)
        {
            echo "Problem with ~query";
        }

        echo "</br>";
        echo "<table border= '1'>";

        while($row = $statement->fetch())
        {
            echo $row['book_id'] . $row['title'];
        }

        echo "</table>";
    }

    ?>
</body>
</html>

You can use columncount property of the PDOStatement object after execute() has been called to determine the number of columns in the resultset.您可以在调用 execute() 后使用 PDOStatement 对象的columncount属性来确定结果集中的列数。

Update: let's see some code更新:让我们看看一些代码

...
$c=$statement->columnCount()-1; //column indexes in resultset are 0 based
while($row = $statement->fetch())
    {
        echo '<tr>'; //start row
        for($i=0; $i<=$c; $i++)
            {
                echo '<td>'.$row[$i] . '</td>'; //print cell
            }
        echo '</tr>'; //close the row
    }
...

To select the number of the columns of a table you can use this query.要选择表的列数,您可以使用此查询。

SELECT COUNT(*) AS total_columns
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'selected_table' AND table_schema = 'your_database';

You can find more info about INFORMATION_SCHEMA here .您可以在此处找到有关 INFORMATION_SCHEMA 的更多信息。

you'd want to use the INFORMATION_SCHEMA.COLUMNS table as indicated in this answer您想使用此答案中所示的 INFORMATION_SCHEMA.COLUMNS 表

SELECT `COLUMN_NAME` 
FROM `INFORMATION_SCHEMA`.`COLUMNS` 
WHERE `TABLE_SCHEMA`='yourdatabasename' 
    AND `TABLE_NAME`='yourtablename';

you first create a loop where you make the <tr><th>[CONTENT]</th>...</tr> row of the table then you run your existing loop to generate the <tr><td>[CONTENT]</td>...</tr> rows.您首先创建一个循环,在其中创建表的<tr><th>[CONTENT]</th>...</tr>行,然后运行现有循环以生成<tr><td>[CONTENT]</td>...</tr>行。

your first loop would more or less be the same as your second with the difference being that you are using a different sql result, using <th> instead of <td> and a loop inside your second loop.您的第一个循环或多或少与您的第二个循环相同,不同之处在于您使用不同的 sql 结果,使用<th>而不是<td>和第二个循环内的循环。 this will...这会...

display the entire table from the database as a table in html将数据库中的整个表显示为 html 中的表

and if you need a row/column count you can stick some sort of iterator in each loop如果您需要行/列计数,您可以在每个循环中粘贴某种迭代器

first loop example第一个循环示例

echo "<tr>";
$countColumn = 0;
while($row = $statement->fetch())
{
    echo "<th>".$row['COLUMN_NAME']."</th>";
    $countColumn += 1;
}
echo "</tr>";

second loop example第二个循环示例

$countRow = 0;
while($row = $statement->fetch())
{
    echo "<tr>";
    foreach($row as $data)
    {
        echo "<td>".$data."</td>";
        $countRow += 1;
    }
    echo "</tr>";
}

NOTE: Browsers are retarded children, unless in your注意:浏览器是弱智儿童,除非在您的

echo "<table border= '1'>";

while($row = $statement->fetch())
{
    echo $row['book_id'] . $row['title'];
}

echo "</table>";

part your $row['book_id'] . $row['title']部分您的$row['book_id'] . $row['title'] $row['book_id'] . $row['title'] has your <tr><td> tags don't let them fill in the blanks in. especially if this is going to be a non-intranet page $row['book_id'] . $row['title']有你的<tr><td>标签,不要让它们填空。特别是如果这将是一个非内联网页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用PHP根据来自两个不同表的表数据生成HTML表单? - How would I generate an HTML form based on table data from two different tables using PHP? 如何从2个SQL表中获取数据并将其放在单个HTML表中? - How do I get data from 2 SQL tables and put it in a single HTML table? 使用php在不同的sql表中导入csv文件的选定列 - importing selected columns of csv file in different sql tables using php 如何让我的 SQL 结果出现在一个表中而不是 PHP/HTML 中的两个表中 - How do I get my SQL results to appear in one table instead of two within PHP/HTML PHP / HTML使用两个表并获取要在html表中使用的名称 - PHP / HTML using two tables and get name to use in html table 如何使用模态和php在html表的特定行上获取值 - How do I get value on a specific row on html table using modal and php 如何使用 PHP 将这些 JSON 数据转换为这样的 HTML 表 - How do I get this JSON data into HTML table like this using PHP 关于如何从Laravel 5.5中的3个不同的SQL表中填充视图中的HTML表的建议? - Suggestions on how I populate my HTML Table in the view from 3 different SQL Tables in Laravel 5.5? 我如何使用PHP将SQL属性回显到HTML表 - How can i echo sql attributes to an HTML table using PHP 如何从PHP SQL中的多个表中获取数据? - How do I get data from multiple table in PHP SQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM