PHP和MYSQLI循环数月

时间:2016-05-02 09:08:26

标签: php json mysqli

我正在尝试检索每个月的数据(01,02,03等),但我不知道如何为它做一个循环。现在我每个月都有这个! (如果已经跳过其他几个月,那么你不必看到所有这些)。

 $queryJanuary = "SELECT count(*) AS num FROM unique_ip WHERE datum BETWEEN '2016-01-01' AND '2016-01-31'";
 $queryFebruari = "SELECT count(*) AS num FROM unique_ip WHERE datum BETWEEN '2016-02-01' AND '2016-02-28'";


     if ($January = $Connection->query($queryJanuary)) {
          $Februari  = $Connection->query($queryFebruari);

            $rowJanuari = mysqli_fetch_assoc($January);
            $rowFebruari  = mysqli_fetch_assoc($Februari);

            $january   = $rowJanuari['num'];
            $februari  = $rowFebruari['num'];

     }else {
         die();
           }

      $data = array($january, $februari);

     echo json_encode($data);

数据库方案:

  1. IP UNSIGNED INT - PK
  2. 基准日期非空
  3. 告诉INT NOT NULL - 默认1

    • datum表示日期
    • 讲述意味着计数
  4. 此外,将查询转换为准备好的语句是否方便或至少是一种好的做法?此代码仅在我和其他管理员可以访问的私人页面上提供。

3 个答案:

答案 0 :(得分:1)

尝试以下查询。这有助于您在单个查询中记录所有月份数。

$queryallmonth = "SELECT MONTHNAME(datum) Month,COUNT(ip) num FROM unique_ip GROUP BY DATE_FORMAT(datum, '%Y%m')"

答案 1 :(得分:0)

使用查询:

SELECT count(*) AS num, EXTRACT(YEAR_MONTH FROM datum) as yearmonth  FROM unique_ip WHERE datum BETWEEN '2016-01-01' AND '2016-05-02' GROUP BY EXTRACT(YEAR_MONTH FROM datum) DESC";

此SQL将为您提供按月计算的数据..

答案 2 :(得分:-1)

我真的不知道你打算做什么,但只是试试你的SQL:

$queryJanuary    = "SELECT count(datum) AS num FROM unique_ip WHERE (datum>='2016-01-01' AND datum<='2016-01-31')";
$queryFebruari   = "SELECT count(datum) AS num FROM unique_ip WHERE (datum>='2016-02-01' AND datum<='2016-02-28')";

$data            = array(); //INITIALIZE DATA TO AN EMPTY ARRAY...
try{
    $January     = $Connection->query($queryJanuary);
    $Februari    = $Connection->query($queryFebruari);

    $rowJanuari  = mysqli_fetch_assoc($January);
    $rowFebruari = mysqli_fetch_assoc($Februari);

    $january     = $rowJanuari['num'];
    $februari    = $rowFebruari['num'];
    $data        = array($january, $februari);
}catch(Exception $e){
    //HANDLE YOUR EXCEPTION HERE...
    die();
}

echo json_encode($data);

您也可以在一个SQL语句中执行所有这些操作:

$queryAll       = "SELECT COUNT(datum) AS countJan,
                   (SELECT COUNT(datum) FROM unique_ip WHERE (datum>='2016-02-01' AND datum<='2016-02-28')) AS countFeb  
                   FROM unique_ip AS af WHERE (datum>='2016-01-01' AND datum<='2016-01-31')";
$data            = array(); //INITIALIZE $data TO AN EMPTY ARRAY...
$rowAll          = array(); //INITIALIZE $rowAll TO AN EMPTY ARRAY...

try{
    $query       = $Connection->query($queryAll);
    $rowAll      = mysqli_fetch_assoc($query);
    $january     = $rowAll['countJan'];
    $februari    = $rowAll['countFeb'];
    $data        = array($january, $februari);
    // BUT YOU DON NOT NEED TO PUSH $january & $februari INTO THE $data ARRAY 
    // BECAUSE YOU HAVE THOSE VALUES ALREADY INSIDE THE $rowAll VARIABLE 
    // WHICH SHOULD CONTAIN THESE KEYS: "countJan" & "countFeb"
}catch(Exception $e){
    //HANDLE YOUR EXCEPTION HERE...
    die();
}

//DUMP THE VALUES TO SEE IF THEY ARE WHAT YOU EXPECTED 
var_dump($data);
var_dump(rowAll);