2

I need to show total count of rows from database ordering by month.

There are my code:

 <?php
 $sql = "SELECT aptName FROM property WHERE status='Sold' GROUP BY MONTH(createdate)";
 if ($result=mysqli_query($con,$sql))
 {
 $rowcount=mysqli_num_rows($result);
 printf("%d\n",$rowcount);
 mysqli_free_result($result);
 }
 ?>

I want to show how many properties sold out in current month. Same thing in next month and so on..

Vinay Hegde
  • 1,424
  • 1
  • 10
  • 23
dmck
  • 23
  • 2
  • Check [this](https://stackoverflow.com/questions/21276975/get-records-of-current-month) it might help you. – Swati Aug 18 '19 at 13:35

2 Answers2

1

If you are using MySQL you could try the following query:

 SELECT count(*) tot_apt_in_month
     FROM property 
     WHERE status='Sold' 
     AND MONTH(createdate) = month(curdate())

To see the result you should at least echo the row content:

  while($row = $result->fetch_array())
  {
  echo $row['tot_apt_in_month'];
  echo "<br />";
  }

or

     "SELECT MONTH(createdate) my_month, count(*) tot_apt_in_month
     FROM property 
     WHERE status='Sold' 
     AND MONTH(createdate) = month(durdate())
     GROUP BY MONTH(createdate)
     ";

  while($row = $result->fetch_array())
  {
      echo $row['my_month'] . " " . $row['tot_apt_in_month'];
      echo "<br />";
  }
Dharman
  • 30,962
  • 25
  • 85
  • 135
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0

You can use below query which will return you multiple records with month and sold property count.

SELECT count(id) soldPropertyCount, MONTHNAME(createdate)
FROM property WHERE status='Sold' GROUP BY MONTH(createdate)

Hope it helps you!!

Rohit Mittal
  • 2,064
  • 2
  • 8
  • 18