php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50259 foreach returning duplicated data
Submitted: 2009-11-21 21:01 UTC Modified: 2009-11-22 00:45 UTC
From: ultrageek1 at hotmail dot com Assigned:
Status: Not a bug Package: MySQL related
PHP Version: 5.2.10 OS: Linux
Private report: No CVE-ID: None
 [2009-11-21 21:01 UTC] ultrageek1 at hotmail dot com
Description:
------------
I loop through MYSQL data with a mysql_data_seek() and a while loop. and I loop through each row returned with a foreach() loop.

function accepts a MYSQL resource as input ($data) along with an optional array of table headers ($head) which might only have one element.

This is PHP 5.2.10, not .11
I would have to ask my hosting provider to upgrade the PHP version.

no variables pass by reference.


Reproduce code:
---------------
<?php
function build_table($data,$head = null){
  echo "<table>";
  if ($head){
    foreach ($head as $h)
      echo "<th>".$h."</th>\n";
  }
  if (is_resource($data)){
    mysql_data_seek($data,0);
    while($row = mysql_fetch_array($data)){
      echo "<tr>";
      foreach($row as $cell){
        echo "<td>".($cell)."</td>";
      }
      echo "</tr>\n";
    }
    echo "</table>\n";
  }
}?>

Expected result:
----------------
 head1 (head2 head3)
r1d1 r1d2 r1d3 r1d4
r2d1 r2d2 r2d3 r2d4
r3d1 r3d2 r3d3 r3d4
....



Actual result:
--------------
 head1 (head2 head3)
r1d1 r1d1 r1d2 r1d2 r1d3 r1d3 r1d4 r1d4
r2d1 r2d1 r2d2 r2d2 r2d3 r2d3 r2d4 r2d4
r3d1 r3d1 r3d2 r3d2 r3d3 r3d3 r3d4 r3d4
....

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-11-21 21:05 UTC] ultrageek1 at hotmail dot com
I loop through the data with mysql_fetch_array() not with mysql_data_seek() sorry.
 [2009-11-22 00:45 UTC] jani@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

Try var_dump() each row..
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 16:01:29 2024 UTC