我试图从两个日期之间的数据库中获取行。我在这里用了其他一些问题来说明这一点,但我不知道如何从这里前进。使用PHP在SQL中的两个日期之间选择行
下面是代码,下面是输出和在SQL中运行查询的照片的屏幕截图。
感谢您的帮助!
-
的代码该第一部分输出ID,姓名等
<?php
$sql = "SELECT * FROM songs ";
$result = $link->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["ID"] . " - Name: " . $row["name"] . " " . $row["released"]. "<br>";
}
} else {
echo "0 results";
}
/* close connection */
$link->close();
?>
的代码输出 “0结果”
<?php
$from_date = '2015-01-01';
$to_date = '2017-04-04';
$sql = "SELECT * FROM songs WHERE released BETWEEN '" . $from_date . "' AND '" . $to_date . "' ";
$result = $link->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["ID"] . " - Name: " . $row["name"] . " " . $row["released"]. "<br>";
}
} else {
echo "0 results";
}
/* close connection */
$link->close();
?>
This is what the page looks like with the above code.
本节I've successfully selected the rows in SQL.
编辑:我把代码写入第二代码块的IF ELSE语句的问题时犯了一个错误。我只是将其更新为我在网站上实际拥有的内容。我没有根据解决方案改变任何东西,但仍然没有得到要打印的行。
',而(){}其他{}'是无效的PHP。 – jhmckimm