Php 格式化fetchAll()的结果

Php 格式化fetchAll()的结果,php,pdo,Php,Pdo,我在sql语句中使用fetchAll(PDO::FETCH_ASSOC)来查询我创建的数据库,目前使用print_r()作为查看数据库的手段。Print\r返回值,例如 Array ( [0] => Array ( [Type] => Poison, Invicibility, Heals [First_Name] => Acton [Last_Name] => Mooney ) [1] => Array ( [Type] => Telepa

我在sql语句中使用fetchAll(PDO::FETCH_ASSOC)来查询我创建的数据库,目前使用print_r()作为查看数据库的手段。Print\r返回值,例如

Array ( [0] => Array ( [Type] => Poison, Invicibility, Heals [First_Name] => Acton [Last_Name] => Mooney ) 
        [1] => Array ( [Type] => Telepathy, Mind-Control [First_Name] => Adam [Last_Name] => Warlock ) 
我尝试使用循环将此信息放入表格中,以使其美观整洁,例如,我希望看到:

         Type                          First_Name    Last_Name
Poison, Invicibility, Heals              Acton         Mooney
Telepathy, Mind-Control                  Adam          Warlock

然而,我目前仍停留在如何实现这一点上。我知道它涉及某种类型的循环,但查询是动态的,有时可能包含3行以上的信息,因此有点混乱。

在读取结果时使用while循环,而不是使用fetchAll()



编辑:基于问题“我的问题是如何知道列号和名称,因为我的查询工作方式是它可以查询任何表”的澄清

//尽可能使用fetchAll
$aResults=$stmnt->fetchAll(PDO::FETCH_ASSOC)
//然后,检查是否有结果
$numResults=计数($aResults);
如果(计数($numResults)>0){
//输出表格标题
回声';
foreach($aResults[0]作为$fieldName=>$value){
echo“”。htmlspecialchars($fieldName)。“”;
}
回声';
//现在输出所有结果
foreach($arresults作为$row){
回声';
foreach($fieldName=>$value的行){
回显“”。htmlspecialchars($value)。“”;
}
回声';
}
//合上桌子
回声';
}否则{
回应“没有结果”;
}
像这样

<?php 

$arr = array ( 0 => array ( 'Type' => 'Poison, Invicibility, Heals', 'First_Name' => 'Acton', 'Last_Name' => 'Mooney' ) ,
        1 => array ( 'Type' => 'Telepathy, Mind-Control', 'First_Name' => 'Adam', 'Last_Name' => 'Warlock' )) ;

echo "<table><tr><th>type</th><th>type</th><th>type</th></tr>";

foreach ($arr as $key=>$val) {
    echo "<tr>";
    echo "<td>".$val['Type']."</td>";
    echo "<td>".$val['First_Name']."</td>";
    echo "<td>".$val['Last_Name']."</td>";
    echo "</tr>";
}        
echo "</table>";      

我的问题是如何知道列号和名称,因为我的查询工作方式是它可以查询任何表。有没有一种方法可以指导循环解析这些信息?不客气。下次再问更清楚的问题;)
// Use fetchAll as you have
$aResults = $stmnt->fetchAll(PDO::FETCH_ASSOC)

// Then, check you have results
$numResults = count($aResults);
if (count($numResults) > 0) {
    // Output the table header
    echo '<table><tr>';
    foreach ($aResults[0] as $fieldName=>$value) {
        echo '<th>' . htmlspecialchars($fieldName) . '</th>';
    }
    echo '</tr>';

    // Now output all the results
    foreach($aResults as $row) {
        echo '<tr>';
        foreach ($row as $fieldName=>$value) {
            echo '<td>' . htmlspecialchars($value) . '</td>';
        }
        echo '</tr>';
    }

    // Close the table
    echo '</table>';
} else {
    echo 'No results';
}
<?php 

$arr = array ( 0 => array ( 'Type' => 'Poison, Invicibility, Heals', 'First_Name' => 'Acton', 'Last_Name' => 'Mooney' ) ,
        1 => array ( 'Type' => 'Telepathy, Mind-Control', 'First_Name' => 'Adam', 'Last_Name' => 'Warlock' )) ;

echo "<table><tr><th>type</th><th>type</th><th>type</th></tr>";

foreach ($arr as $key=>$val) {
    echo "<tr>";
    echo "<td>".$val['Type']."</td>";
    echo "<td>".$val['First_Name']."</td>";
    echo "<td>".$val['Last_Name']."</td>";
    echo "</tr>";
}        
echo "</table>";