如何使用PHP从MySQL数据库中显示五行,然后创建一个新行并显示另外五行等?

有帮助吗?

解决方案

如果您想限制金额,请使用 LIMIT条款查询返回的结果。

如果你想在每五个记录后打印一个<hr/>,你可以通过模数运算符

$counter = 1;

while ($row = mysql_fetch_assoc($rst)) {

 // print $row stuff

 if ($counter % 5 == 0)
    print "<hr />";

 $counter++;

}

基本上,我们有一个变量用于计算我们打印的记录数。一旦该计数器可以除以5,并且不留余数,我们就打印出水平规则。

其他提示

这样的事情可能会有所帮助:

$result = mysql_query($query);  
if($result) {
    while($row = mysql_fetch_assoc($result)) { 
        if(++$i%5==0 && $i>0) {
          /* do the extra thing... new line some styles */
        }
    }
}

呃...你的意思是:

SELECT * FROM `tablename` WHERE ... LIMIT 5
$total = 20;//get total number here;
$limit = 5;
for($i = 0;$i< $total/$limit;$i++)
{
    $sql = $result = $rows = "";
    $start = $limit * $i;
    $sql = "select * from m_table order by id desc limit $start,$limit";
    $result = mysql_query($query);
    while($rows = mysql_fetch_assoc($result))
    {
        //print the result here;
    }
}

每次从mysql获取5行而不一次获取所有行。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top