在php中打印多行数组

时间:2011-12-23 09:26:21

标签: php arrays rows

我有一个可以返回多行的查询。

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

问题是它只打印一行,而我应该打印两行。

有人可以帮助我吗?

5 个答案:

答案 0 :(得分:2)

问题可能是您正在重复使用$row变量。此外,逻辑有点奇怪:为什么每次获取新行时都会遍历$data?你的意思是这样做:

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

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

答案 1 :(得分:0)

尝试类似:

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

答案 2 :(得分: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'>;

答案 3 :(得分:0)

也许问题是你在while循环中迭代$ data数组,我想你想收集$ data中的所有行,并且只有在while break中迭代它才能显示结果。

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

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

还是我误解了?

答案 4 :(得分: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;
        }
    }