0

I have a query which could return multiple rows.

$result = mysql_query($query);
while ($row = mysql_fetch_array($result)){
    $data[] = $row;
    foreach ($data as $row){
       echo $row['...'];
    }
}

The problem is that it only prints one row, while i should have 2 rows printed.

Could someone help me out?

3
  • it will print all the rows, are you sure the query doesn't return a single row? Commented Dec 23, 2011 at 9:28
  • I have entered the query in PhpMyAdmin it does return 2 rows. But when i print the value's i only get 1 row.. Commented Dec 23, 2011 at 9:30
  • It looks like it should print duplicates actually... Like it would print 3 rows if 2 were returned, and then 6 if 3 were returned. (Unless $data is being reset.) Commented Dec 23, 2011 at 9:41

5 Answers 5

2

The problem is probably that you're re-using the $row variable. Also, the logic is a bit strange: why are you looping through $data every time you fetch a new row? Did you mean to do this instead:

while ($row = mysql_fetch_array($result)){
    $data[] = $row;
}

foreach ($data as $row){
   echo $row['...'];
}
Sign up to request clarification or add additional context in comments.

4 Comments

plus the reuse of row shouldn't be a problem.
I think he wants to iterate over the columns - thats why the foreach was inside the while loop
Juhana, thanks.. I have noticed my fault haha.. This did the job! Thanks everybody.(Will accept answer when i can.)
@RageZ @Michael Right, but on the second pass of the while loop he adds another record on $data and does the foreach loop again from the beginning. If there are say 3 results, he'll first display $data[0], then $data[0] and $data[1], and again $data[0], $data[1] and $data[2].
0

Try something like:

$result = mysql_query($query);
while ($row = mysql_fetch_array($result)){
    foreach ($row as $k=>$v){
       echo $k ." = " . $v;
    }
}

Comments

0
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)){
    $data[] = $row;
    foreach ($data as $col){
       echo $col['...'];
    }
    echo '<br>';
}

OR

echo '<table>';
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)){
    $data[] = $row;
    echo '<tr>';
    foreach ($data as $col){
        echo '<td>';
       echo $col['...'];
       echo '</td>';
    }
    echo '</tr>';
}
echo '</table'>;

Comments

0

Maybe the problem is that you iterate the $data array in side the while loop, I think you want to collect all the rows in the $data, and only when while breaks to iterate it to show the results.

$result = mysql_query($query);
while ($row = mysql_fetch_array($result)){
    $data[] = $row;

}
foreach ($data as $row){
       echo $row['...'];
    }

Or did I misunderstood?

Comments

0
    Do some changes in your code for check no. of results

$result = mysql_query($query);



    while ($row = mysql_fetch_array($result)){

        foreach ($row as $val){
           echo $val;
        }
    }

3 Comments

what's the point of checking how many rows as returned? And why would you only want to enter the loop if it was 1?
Might be query returning single row.
That wouldn't matter, the while loop will still enter when 1 row is returned

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.