普通网友 2017-06-10 20:50
浏览 48
已采纳

从数据库中获取某个时间段内可用的每个项目

I want to echo each car ID which is available in a period between the start_date and the end_date. So far i got this with some help here on SO. This gives me a page not responding, if I delete the second part (commented in the script down here) it is eching all the car id's but not only the ones which are available ofcourse. What is wrong with the script? Because i cant figure out what i did wrong here.

<?php
$start_date = '2017-06-12';
$end_date = '2017-06-14';
$items = array();

$result = mysqli_query($con, "SELECT * FROM invoice_line ");
while ($car = mysqli_fetch_array($result)) {
  echo $car['car_car_id'];
  //second part
  $car_available = mysqli_query($con, "SELECT * 
                             FROM invoice_line 
                             WHERE car_car_id = $car['car_car_id'] 
                              and start_date>='$start_date' 
                              and end_date<='$end_date'");
  echo $car_available['car_car_id'];
} // end second part

?>
  • 写回答

2条回答 默认 最新

  • douse8732 2017-06-10 20:58
    关注

    The process you are doing is: 1. fetch all lines from invoice_line 2. for each line, fetch all lines between two dates.

    You do it wrong, because you do not have to use two queries. You can use one:

    <?php
    $start_date = '2017-06-12';
    $end_date = '2017-06-14';
    
    $result = mysqli_query($con, "SELECT car_car_id FROM invoice_line where start_date>='$start_date' AND end_date<='$end_date'");
    while ($car = mysqli_fetch_array($result)) {
      echo $car['car_car_id'];
    } // end second part
    
    ?>
    

    I would add distinct for car_car_id, in order to get only one line per car id. if you want other data frome this line, you have to add it in the "SELECT" query

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)