【问题标题】:How can i display text after every 10 rows of an SQL query?如何在每 10 行 SQL 查询后显示文本?
【发布时间】:2011-03-16 12:56:22
【问题描述】:

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

【问题讨论】:

  • 在每 10 行之后...不是第 10 行抱歉

标签: php sql mysqli


【解决方案1】:

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

【讨论】:

  • 我在围栏上——最简单的方法是使用模运算符,如 Maerlyn 的回答。虽然对于那些不怎么写代码并且不熟悉%的人来说,你的会更容易理解
【解决方案2】:

如果您使用 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】:
    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
    

    【讨论】:

      【解决方案4】:
      $count = 0;
      while (false !== ($row = mysql_fetch_array($result))) {
        //output your row
      
        ++$count;
        if (($count % 10) == 0) {
          //output your special row
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-28
        • 2020-02-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-11
        相关资源
        最近更新 更多