It's late and I'm tired. Hope you'll forgive me if my explanation isn't so good.
As far as I know, you cant do it all in one go - I've used the following function in the past when wanting to display a table in the same way that phpMyAdmin does - i.e with each column name shown above it's data.
Basically, it's one sql statement to get the column names, followed by another to get the data.
Add the two together and presto!
Just substitute mysqli for pdo - its really just the sql and php you need. At least with pdo, you cant use place-holders for table or column names. It may be different with the mysqli extension - I don't know.
function displayTable($tblName)
{
global $pdo;
$colNames = array();
$query = $pdo->prepare("show columns from $tblName");
$query->execute();
while ($result = $query->fetch())
{
$colNames[] = $result[0];
}
echo "<table cellspacing='0'>";
echo "<tbody>";
echo "<tr>";
foreach($colNames as $curName)
{
echo "<th>" . $curName . "</th>";
}
echo "</tr>";
$query = $pdo->prepare("select * from $tblName");
$query->execute();
$rowNum = 0;
while ($result = $query->fetch())
{
if ($rowNum % 2)
printf("<tr class='odd'>");
else
printf("<tr class='even'>");
for ($i=0; $i<count($colNames); $i++)
printf("<td>%s</td>", $result[$i]);
printf("</tr>");
$rowNum++;
}
echo "</tbody>";
echo "</table>";
}