我想查询一个表的列名,放入一个数组,然后将该数据用于 html 的表中,就像这样
<?php
$ser='localhost';
$usr='root';
$pas='password';
$bsd='basedata';
$tab='contact';
try
{
$dbh = new PDO('mysql:host='.$ser.';dbname='.$bsd.'', $usr, $pas);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = $dbh->prepare("SHOW FIELDS FROM $tab ");
$query->execute();
echo "<tr>";
$i=0;
while ($dat = $query->fetch()) {
$nombre_columna[$i]=$dat[0];
echo "<th>".$nombre_columna[$i]."</th>";
$i++;
}
echo "</tr>\n";
$dbh = new PDO('mysql:host='.$ser.';dbname='.$bsd.'', $usr, $pas);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = $dbh->prepare("SELECT * FROM $tab");
$query->execute();
$j=$i;
$i=0;
while ($dat = $query->fetch()) {
echo "<tr>";
while ($i<$j) {
echo "<td>".$dat[$nombre_columna[$i]]."</td>";
$i++;
}
echo "</tr>\n";
}
$dbh=null;
?>
所以当我运行代码时,它只显示查询的第一行,但不再显示