【问题标题】:PHP - Returning an SQL table with foreach statement, only returning the value of the first columnPHP - 使用 foreach 语句返回 SQL 表,只返回第一列的值
【发布时间】:2013-11-27 22:59:43
【问题描述】:

我正在尝试使用 PHP 显示一个 SQL 表,只需传入表名,然后计算出正确显示表的行数和列数。

到目前为止,我已经设法检索到列名,但是我无法让它显示超过第一列的值,如下所示:

ID | lastName | firstname | etc..
10 | 11 | 13 | 16 | 19 | etc..

举个例子。

这是我检索列标题的代码:

    $STH = $conn->prepare("SELECT * FROM $tableName");
    $STH->execute(); 

    $STH = $conn->query("SELECT * FROM $tableName");
    $STH->setFetchMode(PDO::FETCH_ASSOC);

    $headerQuery = $conn->prepare("DESCRIBE employees");
    $headerQuery->execute();
    $table_fields = $headerQuery->fetchAll(PDO::FETCH_COLUMN);

    $num_fields = count($table_fields);

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

    for ($x=0;$x<$num_fields;$x++)
        {
            echo "<th>$table_fields[$x]</th>";
        }

    echo "</tr>";

这里是检索值的代码,它不能正常工作:

for ($x=0;$x<$num_fields;$x++)
        {
            echo "<tr>";
            foreach ($table_fields as &$fieldname) 
                {
                    while($row = $STH->fetch())
                        {
                            echo "<td>" . $row[$fieldname] . "</td>";
                        }
                }
            echo "</tr>";
        }

非常感谢任何帮助,以及任何关于如何更有效地完成我已经完成的工作的建议。

谢谢!

【问题讨论】:

  • $STH 中的查询是什么
  • 糟糕,抱歉 - 我错过了,将其添加到主要问题中。

标签: php mysql sql pdo wamp


【解决方案1】:

我感觉自己像个白痴一样错过了它,我使用了完全错误的变量来计算行数(更不用说循环结构也全错了)

    $fieldValue = $conn->query("SELECT * FROM $tableName"); 
    $fieldValue->setFetchMode(PDO::FETCH_ASSOC); // We'll come back to this later.

    $headerQuery = $conn->prepare("DESCRIBE $tableName"); // Get our table headers from the input table.
    $headerQuery->execute();
    $table_fields = $headerQuery->fetchAll(PDO::FETCH_COLUMN);

    $num_fields = count($table_fields); // Find out how many headers there actually are and make it a useful variable.

    $sql = "SELECT COUNT(*) AS rowscount FROM $tableName"; // Run a count query to find out how many rows are in the table.
    $results = $conn->query($sql)->fetchAll(PDO::FETCH_ASSOC); // n.b. - This comes out as a multi-dimensional array. This is annoying.
    $num_rows = $results[0]['rowscount']; // Get the value out of the array so it's not clogging up the code.

    // echo ("Number of rows: " . $num_rows); // Debugging - this was showing as 0 or 1 for the longest time, until I realised it was multidimensional above.

    echo "<table border='1'><tr>";  // Build the table

    for ($x=0;$x<$num_fields;$x++) // Working through the headers one by one.
        {
            echo "<th>$table_fields[$x]</th>"; // This was the easy bit, displaying the column headers.
        }

    echo "</tr>"; 

    for($x=0;$x<$num_rows;$x++) // Now we need to go down the rows, 
        {
            while($row = $fieldValue->fetch()) // This is where our $fieldValue comes in, pluck out the value of each field before putting it in.
                {
                    echo "<tr>";
                    foreach ($table_fields as &$fieldname) 
                    {
                        echo "<td>" . $row[$fieldname] . "</td>"; 
                    }
                    echo "</tr>";
                }           
        }   
    $conn = null; // Terminate the connection. You're not needed anymore.
    echo "</table>";   //Close the table

【讨论】:

    猜你喜欢
    • 2019-03-22
    • 1970-01-01
    • 2021-08-22
    • 2016-06-03
    • 2015-08-03
    • 2015-10-21
    • 1970-01-01
    • 2020-07-30
    相关资源
    最近更新 更多