PHP-返回带有foreach语句的SQL表,只返回第一列的值


PHP - Returning an SQL table with foreach statement, only returning the value of the first column

我正试图使用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>";
        }

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

谢谢!

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

    $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