在while循环中打印查询,打印一些字段,多次打印一些字段

时间:2016-09-22 11:40:14

标签: php

我想知道如果我想打印我的$ sta和$ stb一次我应该怎么做,但只要我在数据库中找到它们就打印我的$ test,$ sta和$ stb是唯一键,但是有很多$ test in每一个sta和stb

   <?php
    //Establish connection to database
    $host = "*****";
    $conn = *******;
    $query = " Select * 
    from p100f 
    LEfT OUTER JOIN ***** test    ****
    where p100sta = 1 AND p100stb = 1



//Execute query
$queryexe = db2_exec($conn, $query) ;
//Fetch results


while(db2_fetch_row($queryexe)) {

 $sta = db2_result($queryexe, 'p100sta');
 $stb = db2_result($queryexe, 'p100stb');
 $test = ****test******

   print("<div class='text-center'>
    <h2>$sta and $stb</h2>      <---------- this one i want to only print once
    $test       <------------------- this one i want it to repeat aslong
 as i find them in database
</div>");  

现在当我在while循环中同时打印它时。

    $sta
    $stb
    $test

    $sta
    $stb
    $test

    $sta
    $stb
    $test

    $sta
    $stb
    $test

但我希望它像这样打印

    $sta
    $stb
    $test
    $test
    $test
    $test
    $test

任何建议?提前谢谢

1 个答案:

答案 0 :(得分:1)

Try this code


//Execute query
$queryexe = db2_exec($conn, $query) ;
//Fetch results

$i=0;
while(db2_fetch_row($queryexe)) {

 $sta = db2_result($queryexe, 'p100sta');
 $stb = db2_result($queryexe, 'p100stb');
 $test = ****test******

   print("<div class='text-center'>

   if($i==0)
    {
        <h2>$sta and $stb</h2>      <---------- this one i want to only print once
    }
    else
    {
        continue;
    }
    $test       <------------------- this one i want it to repeat aslong
 as i find them in database
</div>");  
$i++;
}//while closed