0
<?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";    

  }

 ?>

pudate 和 dodate 是数据库中的日期。当我尝试运行时,它总是向我显示输出,因为车辆不可用。我正在运行查询以检查这两天之间汽车是否已经被预订,如果汽车已经被预订,它会说汽车不可用,但无论我如何尝试运行查询,它总是给我相同的结果。

4

2 回答 2

0

您未能获取查询的实际结果。实际上,您被调用mysql_query()了两次(返回一个句柄)。在您的情况下,用于mysql_num_rows()获取行数,这应该告诉您车辆是否可用。

于 2013-07-18T00:04:46.980 回答
0

问题是您没有正确处理查询结果。要解决此问题,您可以尝试以下操作:

$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  
}
于 2013-07-18T00:09:20.087 回答