gpt4 book ai didi

php - MySql ORDER By rand() 一行

转载 作者:行者123 更新时间:2023-11-29 11:12:43 30 4
gpt4 key购买 nike

我有这个 php 代码

$coma_count=substr_count($Hob,',');
if ($coma_count==0) {
$query="SELECT * FROM user_opt WHERE Interests='$Hob' LIMIT 80";
}else{
$expl=explode(',',$Hob);
for ($i=0; $i <=$coma_count; $i++) {
$query.=" UNION SELECT * FROM users WHERE Interests LIKE '%{$expl[$i]}%'";
}
}
$sql=$con->query($query." ORDER BY RAND()") or die($con->error);

$coma_count 的数量从 0 到 112 不等,问题是当 $coma_count 为 0 时(意味着仅选择了国家/地区),我的查询将如下所示SQL 不接受 SELECT * FROM user_opt WHERE Country='$Countr' LIMIT 80 ORDER BY RAND()。我能做什么?

最佳答案

创建一个 $limit 变量,根据需要设置它,然后附加它:

$limit = '';
$coma_count=substr_count($Hob,',');
if ($coma_count==0) {
$query="SELECT * FROM user_opt WHERE Interests='$Hob'";
$limit = " LIMIT 80";
}else{
$expl=explode(',',$Hob);
for ($i=0; $i <=$coma_count; $i++) {
$query.=" UNION SELECT * FROM users WHERE Interests LIKE '%{$expl[$i]}%'";
}
}
$sql=$con->query($query." ORDER BY RAND()".$limit) or die($con->error);

关于php - MySql ORDER By rand() 一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40246469/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com