It is given. There is an info_yarluk table and it has a column for which we make selections - info_date_start (date format).
Task. Make a count of the number of records for the month and group weekly.

Came to this request:

$ info_year = date ('Y');
$ info_month = date ('n');

$ sql = "SELECT info_date_start, count (info_date_start), WEEK (info_date_start) FROM info_yarluk WHERE MONTH (info_date_start) = $ info_month AND YEAR (info_date_start) = $ info_year GROUP BY WEEK (info_date_start_Estar_k)

Produces an array:

array(5) { [1]=> int(0) [2]=> string(10) "2016-04-22" [3]=> string(10) "2016-04-01" [4]=> int(0) [8]=> string(10) "2016-04-12" } 

How to make a request?

  • apart from some shortcomings, the query that does not strongly influence the result should already receive what is required. but from where in php the presented array is taken from you, it is completely not corresponding to the above request - I won’t add my mind. Apparently something is namudreno in the php code that this array did - Mike
  • Possible problem in field names. add alias to all computable fields. Although, of course, the structure of the array is very strange - splash58
  • About not understanding the array where it comes from: suggested that it doesn't make sense to upload all the code, although now I understand that it would help solve the problem. Because your comment about the fact that something namudreno in php helped find the problem. - Kanzafarov S.
  • Attached the decision below - Kanzafarov S.

2 answers 2

Do you have two questions in one? right?!

First, the number of records for the month

and second, group the records by week. Although it is not clear what it means to group in this task. We assume that get the number of weeks. If yes, then it is necessary to solve 2 requests

1) number of records by month

  SELECT MONTH(info_date_start) , YEAR(info_date_start), count(info_date_start) FROM info_yarluk GROUP BY MONTH(info_date_start) , YEAR(info_date_start); 

2) group data by week.

  SELECT MONTH(info_date_start) , YEAR(info_date_start), WEEK(info_date_start), count(info_date_start) FROM info_yarluk GROUP BY MONTH(info_date_start) , YEAR(info_date_start),WEEK(info_date_start); 
  • No, the author wants to select entries for a specific month and group by week. your answer to another question is splash58

The solution lay in the field of php. I post the whole piece of code with the solution.

 $info_year = date('Y'); // по умолчанию ГОД текущий $info_month = date('n'); // по умолчанию МЕСЯЦ текущий $sql = "SELECT info_date_start, count(info_date_start), WEEK(info_date_start) FROM info_yarluk WHERE MONTH(info_date_start) = $info_month AND YEAR(info_date_start) = $info_year GROUP BY WEEK(info_date_start)"; $result = $connect->query($sql); while ($count = $result->fetch_array()) { $week_arr[] = $count[1]; } 

I look what we got in the array:

 array(4) { [0]=> string(1) "3" [1]=> string(1) "8" [2]=> string(1) "8" [3]=> string(1) "1" } 

That's right. Manual counting of records showed that the numbers are correct.
Thanks to Mike for pushing the path to the solution.