动态表输出-每个字段在继续之前显示两次


Dynamic table output - each field appears twice before moving on

有人能告诉我为什么在转到下一个值之前,这会产生两次每个字段值吗?

$titles = array('invlineid','invoiceid','quantity','unitprice','itemdesc');
$headers = array('invlineid','invoiceid','quantity','unitprice','itemdesc');
$sql = "SELECT ";
foreach(array_combine($headers, $titles) as $header => $title)
{
    $sql .= "$header as $title,";
}
$sql .= "linetotal as linetotal";
$sql .= " FROM invoicelineitem";
$sql .= " WHERE invoiceid = 1096";
try
{
    $result = $pdo->query($sql);
}
catch (PDOException $e)
{
    $error = 'Error getting invoice line items.---' . $e . '----' . $sql;
    include $_SERVER['DOCUMENT_ROOT'] . '/mincludes/error.html.php';
    exit();
}
if ($result !== false)
{
    $html_table = '<table>';
    $html_table .= '<thead><tr>';
    foreach($titles as $title)
    {
        $html_table .= "<th> $title </th>";
    }
    $html_table .= '</tr> </thead>';
    $html_table .= '<tbody id="dataTable">';
    foreach($result->fetchAll(PDO::FETCH_ASSOC) as $row)
    {
        $html_table .= '<tr>' . "'n";
        foreach($row as $col)
        {
            $html_table .= '<td>';
            $html_table .= '<input type=text name=' . $title;
            $html_table .= ' value=' . $col . '>';
            $html_table .= '</td>' . "'n";
        }
        $html_table .= '</tr>' . "'n";
    }
}
$html_table .= '</tbody> <tr> </table>';

当它到达单个记录的末尾时,它会在表中开始一行新行,但同样,每个值会产生两次。

我认为这是因为你的fetchAll()方法

看这里:http://php.net/manual/en/pdostatement.fetch.php

PDO::FETCH_BOTH(默认值):返回一个由结果集中返回的列名和0索引列编号索引的数组

这样做可以返回一个带有整数索引列号的数组

fetchAll(PDO::FETCH_NUM)