我想在我的网站上显示今天的数据,但如果今天还没有添加数据,我想显示昨天的数据。
这是我正在使用的代码的一部分,现在它显示今天和昨天两个数据是否可用,但我想只在今天显示两者是否可用。
我不知道,如何分开它们。任何人都可以指出我正确的方向吗?
date_default_timezone_set('America/Los_Angeles');
$today = date('Y-m-d');
$yesterday = date('Y-m-d',strtotime("-1 days"));
$result = mysqli_query($bd, "SELECT * FROM apps WHERE datum = '".$today."' OR datum = '".$yesterday."' AND price > 0 ") or die(mysqli_error());
$count = mysqli_num_rows($result);
$cc = 0;
.....
$cc++;
if ($cc < $count) {
echo "\n";
答案 0 :(得分:3)
试试这个:
date_default_timezone_set('America/Los_Angeles');
$today = date('Y-m-d');
$yesterday = date('Y-m-d',strtotime("-1 days"));
$result = mysqli_query($bd, "SELECT * FROM apps WHERE datum = '".$today."' AND price > 0 ") or die(mysqli_error());
$count = mysqli_num_rows($result);
if($count == 0) {
$result = mysqli_query($bd, "SELECT * FROM apps WHERE datum = '".$yesterday."' AND price > 0 ") or die(mysqli_error());
$count = mysqli_num_rows($result);
}
$cc = 0;
.....
$cc++;
if ($cc < $count) {
echo "\n";
答案 1 :(得分:2)
您需要在查询末尾添加ORDER BY datum DESC LIMIT 1
并阅读单行。
$query = "SELECT *
FROM apps
WHERE (datum = '".$today."'
OR datum = '".$yesterday."')
AND price > 0
ORDER BY datum DESC
LIMIT 1";
$result = mysqli_query($bd, $query) or die(mysqli_error());
if ($row = mysqli_fetch_row($result)) {
// You have the data here
}