我有这段代码:
$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")
如何计算总数?
(附注:$i必须设置为 0)
简单:
$counter = 0;
while(..) {
$counter++; // or $counter = $counter + 1;
}
echo $counter;
定义循环外部的变量。
这是您的原始查询:
$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")
通过添加 COUNT
命令,它将计算SUM
中考虑的行数:
SELECT SUM(value), COUNT(value) FROM...
然后,当您取回MYSQL_RESULT
时,您需要获取数据:
$data = mysql_fetch_array($combined7);
然后,这将具有以下数组:
Array(
[0] = SUM
[1] = COUNT
)
注意:mysql_*
已被弃用。请改用mysqli_*
或PDO<</p>
正确理解你的问题..但我认为您想计算更新的总行
数 $sum=0;
while(){
$sum += mysql_num_rows($combined7); //here it will add total upadted row in $sum...
print $sum; // if you want to print every time total
}
print $sum; // if you want to print only one time total
您应该在 while 之前定义一个值为 0 的变量。 然后在 while 内递增此变量的值。 然后在 while 结束后打印此变量。
$start_date = date("Y/m/d");
$end_date = date('Y/m/d', strtotime($start_date . " -7 days"));
$sn = 0;
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());
$sn += mysql_num_rows($combined7);
endwhile;
print $sn;