2

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.

Todd Bauer
  • 219
  • 1
  • 7
  • 15

1 Answers1

3

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);
jeroen
  • 91,079
  • 21
  • 114
  • 132