简体   繁体   中英

php - count total num rows inside while loop

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)

How can I count the total number?

(PS $i have to be set to 0)

Simple:

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

echo $counter;

define the variable outside the loop.

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

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.

      $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;

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 :

SELECT SUM(value), COUNT(value) FROM...

Then, when you get the MYSQL_RESULT back, you need to fetch the data:

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM