我试图根据我的数据库中的开张和关门时间来显示店铺是开还是关。如果是打开的,则显示当天的打开和关闭时间;如果是关闭的,则显示当天的打开和关闭时间。我现在的问题是,即使商店计划开张(尝试1)或根本不响应任何东西(尝试2),我的查询仍在回显关闭。
我的数据库中关闭的商店表示为00:00。任何建议或指导都将非常感谢,因为我正在自学,并且已经停滞不前。
数据库
CREATE TABLE `Opening_hrs` (
`OH_ID` bigint(255) NOT NULL AUTO_INCREMENT,
`Restaurant_ID` bigint(255) NOT NULL,
`Day_of_week` int(11) NOT NULL,
`Open_time` time NOT NULL,
`Closing_time` time NOT NULL,
PRIMARY KEY (`OH_ID`),
KEY `Restaurant_ID` (`Restaurant_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8这是我第一次尝试
date_default_timezone_set("Europe/London");
$output_ohr = '';
$ohrs = mysqli_query($dbc, "SELECT * FROM Opening_hrs
WHERE Restaurant_ID='$rest_id' AND Day_of_week = DATE_FORMAT(NOW(), '%w')
AND CURTIME() BETWEEN Open_time AND Closing_time");
echo var_dump($ohrs);
$count_ohrs = mysqli_num_rows($ohrs);
if ($count_ohrs === 0) {
$output_ohr = '<b> Closed</b>';
} else {
$i = 1;
}while ($row_ohr = mysqli_fetch_array($ohrs )) {
$o_time = $row_ohr['Open_time'];
$c_time = $row_ohr['Closing_time'];
$output_ohr = $output_ohr . '<p>Open</p>' .
'<p>' .$o_time. ' - ' .$c_time. '</p>'
;
$i++;
}我的第二次尝试
date_default_timezone_set("Europe/London");
$closed= strtotime("00:00am today GMT");
$output_ohr = '';
$ohrs = mysqli_query($dbc, "SELECT * FROM Opening_hrs
WHERE Restaurant_ID='$rest_id' AND Day_of_week = DATE_FORMAT(NOW(), '%w')
AND CURTIME() BETWEEN Open_time AND Closing_time");
echo var_dump($ohrs);
$i = 1;
while ($row_ohr = mysqli_fetch_array($ohrs )) {
$o_time = $row_ohr['Open_time'];
$c_time = $row_ohr['Closing_time'];
if($o_time === $closed){
$output_ohr = '<p>closed</p>';
}else{
$output_ohr = $output_ohr . '<p>Open</p>' .
'<p>' .$o_time. ' - ' .$c_time. '</p>'
;
$i++;
}
} 发布于 2016-05-24 05:09:10
在您的第二次尝试中,您只选择了开放的商店。如果商店关闭,查询将不会返回任何行,因此while块甚至不会运行一次。
使用以下代码查看商店是开着还是关着:
$stmt = mysqli_prepare($dbc, "SELECT COUNT(*) FROM Opening_hrs
WHERE Restaurant_ID=? AND Day_of_week = DATE_FORMAT(NOW(), '%w')
AND CURTIME() BETWEEN Open_time AND Closing_time");
$stmt->bind_param('i', $rest_id);
$stmt->execute();
$stmt->bind_result($result);
$stmt->fetch();
if ($result) {
echo 'Open';
} else {
echo 'Closed';
}(请注意prepared statements的用法,它可以防止可能的SQL注入。)
https://stackoverflow.com/questions/37400170
复制相似问题