dqol6556 2017-03-06 10:23
浏览 29
已采纳

如何将SQL查询与PHP脚本相结合

I have this query to count the number of forms that are submitted by a certain user

$sql="SELECT `ID` FROM `kform` WHERE `ID`='$ID'";                               
$result = $conn->query($sql);
$k=0;
if ($result->num_rows > 0) {
   while($row = $result->fetch_assoc()) {                                                                                                 
      $k++;
   }
}

plus I have this php script that is basically check a certain date (which is included in each form) and prints valid if it is within this fiscal year

$endYear = 2017;
while($endYear <= 2025) {
    $end = $endYear.'/06/30';
    $endDate = DateTime::createFromFormat('Y/m/d', $end);
    $initDate = DateTime::createFromFormat('Y/m/d', $end);
    $initDate = $initDate->sub(new DateInterval('P1Y')) -> add(new DateInterval('P1D'));
    $ddb =  $row2['Date'];
    $dateFromDB = DateTime::createFromFormat('Y-m-d', $ddb);
    if ($dateFromDB >= $initDate && $dateFromDB <= $endDate) { 
        echo "valid
";
        echo "\tStartDate->\"".$initDate->format("Y-m-d")."\"
";
        echo "\tEndDate->\"".$endDate->format("Y-m-d")."\"
";
        echo "\tDateFromDatabase->\"".$dateFromDB->format("Y-m-d")."\"
";
    }

    $endYear++;
}

Now what I was trying to do is to combine these two functions together, the idea behind that is to count only the forms that are in this fiscal year, any older forms should not be counted. I tried different ways to combine them but it gave me different errors every time, So is it even possible to do so?

  • 写回答

1条回答 默认 最新

  • doupai8095 2017-03-06 10:33
    关注

    Go on from that:

     function isValidYear($dategiven){
       $endYear=2017;
       while($endYear<=2025) {
         $end = $endYear.'/06/30';
         $endDate = DateTime::createFromFormat('Y/m/d', $end);
         $initDate = DateTime::createFromFormat('Y/m/d', $end);
         $initDate = $initDate->sub(new DateInterval('P1Y'))->add(new DateInterval('P1D'));
         $ddb =  $dategiven;
         $dateFromDB = DateTime::createFromFormat('Y-m-d', $ddb);
         if ($dateFromDB>=$initDate && $dateFromDB<=$endDate) { 
           return true;
         }
         $endYear++;
        }
        return false;
     }
    

    And

    $sql="SELECT `ID` FROM `kform` WHERE `ID`='$ID'";                               
    $result = $conn->query($sql);
    $k=0;
    if ($result->num_rows > 0) {
     while($row = $result->fetch_assoc()) {
       if(isValidYear($row['datefield'])){
          $k++;
       }
    }
    

    Note: Your $sql selects only the ID field. You have to at Date

    $sql="SELECT * FROM `kform` WHERE `ID`='$ID'"; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器