简体   繁体   English

如何从数据库中查找特定日期是否位于两个日期之间?

[英]how to find whether the a particular date lies between two dates from the database?

<?php

 $hostname = "localhost";
 $db_user = "root";  
 $db_password = "";  
 $database = "rentcar";  
 $db_table = "reservation"; 

 $db = mysql_connect($hostname, $db_user, $db_password); 

 mysql_select_db($database,$db);

 $date1 = $_POST['datepicker1'];

 $date2 = $_POST['datepicker2'];


 $start_date = strtotime($date1);

 $end_date = strtotime($date2);

 $query  =  mysql_query("SELECT * FROM reservation WHERE  ('pudate'>".$start_date." AND         
 'dodate'<".$end_date.") OR ('pudate'<".$start_date." AND 'dodate'>".$end_date." ) OR 
  ('pudate'<".$end_date." AND 'dodate'>".$end_date.") OR ('pudate'<".$start_date." AND 
  'dodate'>".$start_date." )");

 $result = mysql_query($query); 

  if ($result) 

 {
   echo "vehicle is available";
 }

 else
 {
 echo "vehicle is not available";    

  }

 ?>

the pudate and dodate are the dates in the database. pudate和dodate是数据库中的日期。 When i try to run it always show me the output as vehicle is not available. 当我尝试运行时,始终向我显示输出,因为车辆不可用。 I m running the query st check whether the car is already booked between two these days if the car is already booked it will say the car is not available but no matter how i try to run the query it always give me the same result. 我正在运行查询st,检查这两天之间是否已经预订了汽车(如果已经预订了汽车),它将说汽车不可用,但是无论我如何尝试运行查询,它总是会给我相同的结果。

You failed to pick up the actual result of the query. 您未能获得查询的实际结果。 Actually, you are call mysql_query() twice (which returns a handle ). 实际上,您两次调用mysql_query() (返回一个handle )。 In your case, use mysql_num_rows() to fetch the amount of rows, which should tell you if the vehicle is available or not. 在您的情况下,请使用mysql_num_rows()获取行数,该行数将告诉您该载具是否可用。

The problem is that you are not correctly processing the results of your query. 问题是您没有正确处理查询结果。 To fix this, you can try something like this: 要解决此问题,您可以尝试执行以下操作:

$result =  mysql_query("SELECT * FROM reservation WHERE  ('pudate'>".$start_date." AND         
  'dodate'<".$end_date.") OR ('pudate'<".$start_date." AND 'dodate'>".$end_date." ) OR 
  ('pudate'<".$end_date." AND 'dodate'>".$end_date.") OR ('pudate'<".$start_date." AND 
  'dodate'>".$start_date." )");

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    //$row contains the results of your query  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM