如何在每10行SQL查询后显示文本?

时间:2010-07-15 16:59:56

标签: php sql mysqli

说我有一个查询“从用户中选择用户名”。我想将此查询输出到PHP页面,但在每隔10行后我想显示自己的自定义文本。有任何想法吗?它将从第11个记录继续。

4 个答案:

答案 0 :(得分:8)

$count = 0;
while (false !== ($row = mysql_fetch_array($result))) {
  //output your row

  ++$count;
  if (($count % 10) == 0) {
    //output your special row
  }
}

答案 1 :(得分:1)

最简单的方法是将数据表拉回来,循环遍历行,将每个行写出来,同时跟踪您所在的行。然后每十次迭代,写出你的自定义消息。

答案 2 :(得分:0)

如果您使用PDO运行mysql查询,则可以在PHP中创建变量,然后通过该变量限制查询。

这是一个不完整的例子,但你可能会得到这个想法。

<?php
  $first = 0;
  $second = 9;
  $stmt = $db->prepare('select username from users limit :first, :second');

  $stmt->bindParam(':first', $first);
  $stmt->bindParam(':second', $second);

  $stmt->execute();

  #loop through your results here and then have a custom message, 
  #then change your variable values and execute the statement again. 
  #Repeat this until there are no more rows.   

?>

答案 3 :(得分:0)

create table #t
(
    UserName varchar(100)
)

declare @count int
declare @rows int
set @rows = 0

select @count = count(*) from  users
while (@count > 0 )
begin
    insert into #t
    select top 10 username from users where userid > @rows

    insert into #t select '******'

    set @count = @count - 1
    set @rows = @rows + 10
end

select * from #t

drop table #t