php - 计算while循环内的总行数

标签 php while-loop

我有这段代码:

  $i=0;
      $start_date = date("Y/m/d");
      $end_date = date('Y/m/d', strtotime($start_date . " -7 days"));



      while($days7=mysql_fetch_assoc($q)): 
          $next_date = strtotime($i--." days", strtotime($start_date));
          $date = date("Y/m/d",$next_date); 
      #Let's get the latest click combined from the latest 7 days
          $combined7=mysql_query("SELECT sum(value) FROM `xeon_stats_clicks` WHERE user='".$userdata['username']."' AND typ='4' AND data='$date' ORDER BY data DESC LIMIT 8") or die(mysql_error());              


      print mysql_num_rows($combined7);

      endwhile;  

我需要看看 $combined7 有多少行正在得到。
目前,我正在使用 print mysql_num_rows($combined7);但这只是打印出:1 1 1 1 1(每行的数字“1”)

我如何计算总数?

(PS $i 必须设置为 0)

最佳答案

简单的:

$counter = 0;
while(..) {
      $counter++; // or $counter = $counter + 1;
}

echo $counter;

在循环外定义变量。

关于php - 计算while循环内的总行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19028057/

相关文章:

php - foreach如何遍历数组?

php - 如何在 Laravel Socialite 中使用 Oauth2 Provider 代替 Oauth1?

php - 将 PDO sqlite 查询转换为数组

java - 什么是NumberFormatException,我该如何解决?

java - 如果值相同则停止 while 循环 - Java

bash - 在 Firefox 的 newtabs 中打开文件中的 url

php 脚本无法读取带有冒号的 xml 数据 (:)

java - 在 solr 中进行分面时出现问题

c - 为什么这个c程序会陷入死循环?

python - While 循环 - 字符串索引超出范围?