Quick PHP/MySQL question:
I'm trying to display the data from a MySQL database table as an HTML table, and for some reason my code doubling the output of each piece of data.
This is my code:
$rowarray = $statement->fetchall();
print "<tr>\n";
foreach ($rowarray as $row) {
foreach ($row as $col) {
print "\t<td>$col</td>\n";
}
print "</tr>\n";
}
My results look something similar to this:
User ID | User Name | First Name | Last Name
1 1 User Name User Name First Name First Name Last Name Last Name
Etc etc. You get the idea. Why this is happening? By the way, if I manually add the column information by referring to row[] subscripts 0-3, everything is displayed properly; it's only when I use the nested foreach statements that the data is duplicated.
You are getting both a numeric and a name-indexed value back from PDO fetchall. To get just the numeric index:
$rowarray = $statement->fetchall(PDO::FETCH_NUM);
来源:https://stackoverflow.com/questions/8393034/php-displaying-table-data-using-fetchall