3

this one is really puzzling me, so thanks for any help. I am trying to output the values of a SQL result set, but the foreach loop appears to be iterating twice, then incrementing the array index. Such that if my DB row holds the contiguous integers 1,2,3,4,5 the output of the foreach loop is 1,1,2,2,3,3,4,4,5,5. Can't see the reason. Code follows.

$result = mysql_query("SELECT * FROM Test");

echo "<table border=1><tr></tr>";

While($row = mysql_fetch_array($result))
{
    echo "<tr>";
    foreach($row as $value)
    {
        echo "<td>" . $value . "</td>";
    }
    echo "</tr><tr>";
    for($x=0;$x<5;$x++)
    {
        echo "<td>" . $row[$x] . "</td>";
    }
    echo "</tr>";
}

The second for loop was thrown in afterwards for debugging, to make sure the array didn't actually contain two instances of each value. But that loop outputs as expected: 1,2,3,4,5. Any Ideas? Thanks.

1
  • Seing as you are having trouble with this, I would suggest using the mysqli functions instead, so in your case you wouldn't need to reference row in the foreach loop, but just use each individual row as you go through it. Just a suggestion. Commented Jan 13, 2012 at 3:49

4 Answers 4

4

By default, mysql_fetch_array's second parameter is set to MYSQL_FETCH_BOTH, which will register each value under the index as well as the name of the column. Supply MYSQL_FETCH_NUM to get only numeric indices:

while($row = mysql_fetch_array($result, MYSQL_FETCH_NUM)) {
 ...
Sign up to request clarification or add additional context in comments.

Comments

0

mysql_fetch_array returns an associative array using the column names of the mysql result as keys, but it also leaves them accessible by column number, so the row:

Mysql COL: A|B|C Mysql VAL: 1|2|3

will return an array that looks like this after going through the mysql_fetch_array function:

array(
   'A'=>1,
   0  =>1,
   'B'=>2
   1  =>2,
   'C'=>3,
   2  =>3
)

and the foreach loop will iterate through the defined and numeric keys (producing the duplication)

1 Comment

Thank you as well. The array illustration was most useful.
0

The other option is to do a foreach loop that calls both the key and the value.

foreach($row as $key=>$value)

3 Comments

Nope, that will still enumerate all keys, and therefore each cell twice.
Good to know. I have been using it within my classes and have not had an issue. Thanks for pointing that out.
Using foreach with keys and values is an excellent practice, nothing wrong with it at all. But the OP does not want the full array, although he didn't realize that ;)
0

change

    While($row = mysql_fetch_array($result))
{
    echo "<tr>";
    foreach($row as $value)
    {
        echo "<td>" . $value . "</td>";
    }
    echo "</tr><tr>";
    for($x=0;$x<5;$x++)
    {
        echo "<td>" . $row[$x] . "</td>";
    }
    echo "</tr>";
}

to

    While($row = mysql_fetch_array($result))
{
    echo "<tr>";
    foreach($row as $value)
    {
        echo "<td>" . $value . "</td>";
    }
    echo "</tr>";
}

and tell me what your output is...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.