【发布时间】:2021-11-19 03:24:12
【问题描述】:
我想从我的数据库中获取特定的班次时间,但此脚本返回“0 个结果”;
这是我的数据库
id | shift_title | start_time | end_time |
------------------------------------------
1 | shift1 | 00:00 | 08:30 |
2 | shift2 | 08:30 | 12:00 |
3 | shift3 | 12:00 | 19:30 |
4 | shift4 | 19:00 | 00:00 |
这是我的 php 脚本
include 'config.php';
$create_time = date('H:i');
$sql = "SELECT * FROM shift_times WHERE start_time <= '$create_time' AND end_time > '$create_time'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["shift_title"];
}
} else {
echo "0 results";
}
$conn->close();
【问题讨论】:
-
警告:您对SQL Injections 持开放态度,应该使用参数化的prepared statements,而不是手动构建查询。它们由PDO 或MySQLi 提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行,you are still in risk of corrupting your data。 Escaping is not enough!
-
尝试
start_time而不是$start_time。它是列名,而不是变量。 $end_time 也一样 -
我试过但仍然返回同样的问题
-
start_time和end_time的列类型是什么? -
您正在尝试对字符串进行基于时间的比较。 Mysql 不知道该怎么做。您需要将您的列设为TIME 或其他一些date-related 列类型。