我有一段代码:
$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);,但这只是打印出来:1111-1(每一行的数字'1‘)
我怎么算总数?
(P.S. $i必须设置为0)
发布于 2013-09-26 12:21:29
简单:
$counter = 0;
while(..) {
$counter++; // or $counter = $counter + 1;
}
echo $counter;定义循环外部的变量。
发布于 2013-09-26 12:40:14
以下是您的原始查询:
$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
发布于 2013-09-26 12:21:41
我没听清楚你的问题..。但是我想你想要计算更新后的全部行
$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 totalhttps://stackoverflow.com/questions/19028057
复制相似问题