Php 如果不存在更高的id,如何将mysql where子句设置为返回id=1
我需要在代码中的某个地方说“如果在当前id之后没有找到任何内容,请从idPhp 如果不存在更高的id,如何将mysql where子句设置为返回id=1,php,mysql,Php,Mysql,我需要在代码中的某个地方说“如果在当前id之后没有找到任何内容,请从id1”开始搜索。我遇到了一个问题,例如,我正在查询任何id>4,其中status==1。由于id5的状态不正确,因此代码无法执行。我需要它回到id#1 mysql表示例 ----------------------------------------- | id | page | status | referral | ----------------------------------------- | 1
1
”开始搜索。我遇到了一个问题,例如,我正在查询任何id>4,其中status==1。由于id5
的状态不正确,因此代码无法执行。我需要它回到id#1
mysql表示例
-----------------------------------------
| id | page | status | referral |
-----------------------------------------
| 1 | new | 1 | new.php |
| 2 | used | 0 | used.php |
| 3 | lease | 1 | lease.php |
| 4 | video | 0 | video.php |
| 5 | serv | 0 | serv.php
-----------------------------------------
<?php
$referral = $_SERVER['HTTP_REFERER'];
$sql = "SELECT * FROM allpages
WHERE status = '1' && referral != '$referral' && id > '4'";
$result = mysqli_query($conn, $sql);
$num = mysqli_num_rows($result);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
if ($row['status'] == 1){
echo "<script>
setTimeout(function() {
window.location.href = '" . $row['url'] . "';
}, 10000);
</script>";
break;
}else { echo "no page to display"; }
}
}
mysqli_close($conn);
?>
php代码
-----------------------------------------
| id | page | status | referral |
-----------------------------------------
| 1 | new | 1 | new.php |
| 2 | used | 0 | used.php |
| 3 | lease | 1 | lease.php |
| 4 | video | 0 | video.php |
| 5 | serv | 0 | serv.php
-----------------------------------------
<?php
$referral = $_SERVER['HTTP_REFERER'];
$sql = "SELECT * FROM allpages
WHERE status = '1' && referral != '$referral' && id > '4'";
$result = mysqli_query($conn, $sql);
$num = mysqli_num_rows($result);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
if ($row['status'] == 1){
echo "<script>
setTimeout(function() {
window.location.href = '" . $row['url'] . "';
}, 10000);
</script>";
break;
}else { echo "no page to display"; }
}
}
mysqli_close($conn);
?>
您希望找到大于4
的第一个id
,否则返回表中的第一个id
。您可以通过按(id>4)DESC
排序,然后按id ASC
排序来完成此操作。这将确保所有id的>4
都显示在id 1之前。同时从where子句中删除id>4
。例如:
SELECT * FROM allpages WHERE status = '1' ORDER BY (id > '4') DESC, id ASC LIMIT 1
您希望找到大于4
的第一个id
,否则返回表中的第一个id
。您可以通过按(id>4)DESC
排序,然后按id ASC
排序来完成此操作。这将确保所有id的>4
都显示在id 1之前。同时从where子句中删除id>4
。例如:
SELECT * FROM allpages WHERE status = '1' ORDER BY (id > '4') DESC, id ASC LIMIT 1
除了有关SQL注入的安全原因外,您还必须了解,如果没有给出行,您必须从一开始就请求其他SQL请求来获取数据
在这种情况下,您需要一个类似SQL的
SELECT * FROM allpages WHERE status = '1' && referral != '$referral' ORDER BY id ASC LIMIT 1;
这就行了。请记住,如果使用函数获取sql结果,代码可能会更好、更少
<?php
$referral = mysqli_real_escape_string($conn, $_SERVER['HTTP_REFERER']);
$sql = "SELECT * FROM allpages WHERE status = '1' && referral != '$referral' && id > '4' LIMIT 1";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result));
if (!empty($row) && is_array($row)) {
// result found
echo "<script>
setTimeout(function() {
window.location.href = '" . $row['url'] . "';
}, 10000);
</script>";
} else {
// no result yet, start search from beginning
$sql2 = "SELECT * FROM allpages WHERE status = '1' && referral != '$referral' ORDER BY id ASC LIMIT 1";
$result2 = mysqli_query($conn, $sql2);
$row2 = mysqli_fetch_assoc($result2));
if (!empty($row2) && is_array($row2)) {
// result found by start from beginning
$row2 = mysqli_fetch_assoc($result2));
echo "<script>
setTimeout(function() {
window.location.href = '" . $row2['url'] . "';
}, 10000);
</script>";
} else {
// noting found from start
echo "no page to display";
}
}
mysqli_close($conn);
?>
除了有关SQL注入的安全原因外,您还必须了解,如果没有给出行,您必须从一开始就请求另一个SQL请求来获取数据
在这种情况下,您需要一个类似SQL的
SELECT * FROM allpages WHERE status = '1' && referral != '$referral' ORDER BY id ASC LIMIT 1;
这就行了。请记住,如果使用函数获取sql结果,代码可能会更好、更少
<?php
$referral = mysqli_real_escape_string($conn, $_SERVER['HTTP_REFERER']);
$sql = "SELECT * FROM allpages WHERE status = '1' && referral != '$referral' && id > '4' LIMIT 1";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result));
if (!empty($row) && is_array($row)) {
// result found
echo "<script>
setTimeout(function() {
window.location.href = '" . $row['url'] . "';
}, 10000);
</script>";
} else {
// no result yet, start search from beginning
$sql2 = "SELECT * FROM allpages WHERE status = '1' && referral != '$referral' ORDER BY id ASC LIMIT 1";
$result2 = mysqli_query($conn, $sql2);
$row2 = mysqli_fetch_assoc($result2));
if (!empty($row2) && is_array($row2)) {
// result found by start from beginning
$row2 = mysqli_fetch_assoc($result2));
echo "<script>
setTimeout(function() {
window.location.href = '" . $row2['url'] . "';
}, 10000);
</script>";
} else {
// noting found from start
echo "no page to display";
}
}
mysqli_close($conn);
?>
您的SQL易受攻击。使用准备好的语句。在查询中添加限制1
是个好主意。除此之外,您还需要删除while循环,因为您只对第一行感兴趣。因此,删除,同时删除if
周围的,删除break
语句,并将limit 1
添加到查询中,以修复SQL易受攻击的第二个问题。使用准备好的语句。在查询中添加限制1
是个好主意。除此之外,您还需要删除while循环,因为您只对第一行感兴趣。因此,删除,同时删除if
周围的,删除break
语句,并将limit 1
添加到查询中,以解决第二个问题我在回答中没有想到这一点。这样会容易得多。说得好。我发现这是最好的答案。我唯一添加的是查询末尾的限制1。非常感谢你的帮助!我在回答时没有想到这一点。这样会容易得多。说得好。我发现这是最好的答案。我唯一添加的是查询末尾的限制1。非常感谢你的帮助!我无法运行此代码,尝试了一些方法,但页面上无法加载代码。我最终使用了下面的Max Meijer答案。我很感谢你抽出时间来帮忙!我无法运行此代码,尝试了一些方法,但页面上无法加载代码。我最终使用了下面的Max Meijer答案。我很感谢你抽出时间来帮忙!