PHP - 使用foreach语句返回SQL表,仅返回第一列的值

时间:2013-11-27 22:59:43

标签: php mysql sql pdo wamp

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

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

谢谢!

1 个答案:

答案 0 :(得分:0)

我觉得这样的白痴错过了它,我使用完整的错误变量来计算行数(更不用说循环结构也是错误的)

    $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