php - 计算while循环内的总行数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19028057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php - count total num rows inside while loop
提问by oliverbj
I've got this piece of code:
我有这段代码:
$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;
I need to see how many rows that $combined7
is getting.
Currently, I am using print mysql_num_rows($combined7);
but that just prints out: 1 1 1 1 1 (The number '1' for each row)
我需要看看有多少行$combined7
。目前,我正在使用print mysql_num_rows($combined7);
但只是打印出:1 1 1 1 1(每行的数字“1”)
How can I count the total number?
我如何计算总数?
(P.S. $i have to be set to 0)
(PS $i 必须设置为 0)
回答by q0re
Simple:
简单的:
$counter = 0;
while(..) {
$counter++; // or $counter = $counter + 1;
}
echo $counter;
define the variable outside the loop.
在循环外定义变量。
回答by UnholyRanger
Here is your original query:
这是您的原始查询:
$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")
By adding the COUNT
command, it will count the amount of rows that were considered in the SUM
:
通过添加COUNT
命令,它将计算在 中考虑的行数SUM
:
SELECT SUM(value), COUNT(value) FROM...
Then, when you get the MYSQL_RESULT
back, you need to fetch the data:
然后,当你得到MYSQL_RESULT
返回时,你需要获取数据:
$data = mysql_fetch_array($combined7);
This will then have the following array:
这将具有以下数组:
Array(
[0] = SUM
[1] = COUNT
)
NOTICE: mysql_*
has been deprecated. Please use mysqli_*
or PDO instead
注意:mysql_*
已被弃用。请改用mysqli_*
或 PDO
回答by Kalpit
i didn't get your question proper.. but i think you want to count total updated row
我没有正确回答你的问题..但我认为你想计算更新的总行数
$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
回答by Md. Sahadat Hossain
you should define a variable with value 0 before while. then you increment value of this variable inside the while. then you print this variable after end of while.
您应该在 while 之前定义一个值为 0 的变量。然后你在 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;