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.

0

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.