CREATE TABLE IF NOT EXISTS `guest_details` (
`name` varchar(15) NOT NULL,
`email` varchar(15) NOT NULL,
`cell` varchar(15) NOT NULL,
`roomtype` varchar(15) NOT NULL,
`checkin` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`checkout` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`adults` varchar(15) NOT NULL,
`numberofrooms` varchar(15) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `guest_details` (`name`, `email`, `cell`, `roomtype`, `checkin`, `checkout`, `adults`, `numberofrooms`)
VALUES
('sumanth', 'sumanth@gmail.c', '99999999999999', 'Single_AC', '2014-09-15 00:00:00', '2014-09-19 00:00:00', 'FGDFG', '2'),
('vijey', 'vijey.sk@gmail.', '222222222222222', 'Double_AC', '2014-09-21 00:00:00', '2014-09-24 00:00:00', 'lpuio', '1');
我需要在签入和签出日期之间从数据库中检索数据。我的问题是日期不能比较。我也在检查mysql。我需要解决的问题是什么。
$res = "select * from guest_details where checkin>=$date1 AND checkout<=$date1";
$res1 = mysql_query($res);
if ($res1)
/*while($row = mysql_fetch_array($res1))*/
{
echo $date1;
}
?>
答案 0 :(得分:0)
您正在比较$date1
值而不引用单引号,因此您的结果查询就像
checkin>= 2014-09-18 AND checkout<= 2014-09-18
产生错误的必须是
select * from guest_details where checkin>='$date1' AND checkout<= '$date1'
使用占位符更好的参数
$dbh = new PDO('mysql:host=yourhost;dbname=dbname', 'user', 'pass');
$sth = $dbh->prepare("select * from guest_details where
checkin >= :date AND checkout <= :date
");
$sth->bindParam(':date', $date1, PDO::PARAM_STR);
$sth->execute();
$result = $sth->fetchAll();
foreach($result as $res){
echo $res['your_date_column_name'];
echo $res['another_column'];
}
同样在开发过程中,您应该在执行
之前关注错误$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
if (!$sth) {
echo "\nPDO::errorInfo():\n";
print_r($dbh->errorInfo());
}
答案 1 :(得分:0)
$from_date = $_POST['from_date'];
$to_date = $_POST['to_date'];
$sql = "select * from guest_details where curdate() between '$from_date' and '$to_date' ";
$query = mysql_query($sql);
while($res_data = mysql_fetch_assoc($query)){
echo $res_data['fields_name'];
}
N.B。 CURDATE()
返回当前日期并与$from_date
和$to_date
的值进行比较的位置。 curdate()
的工作方式如下:if(curdate() <= $from_date and $to_date) return true;
否则为假。与pc当前日期进行比较。