gpt4 book ai didi

php - SQL查询中关键字内容的动态

转载 作者:行者123 更新时间:2023-11-29 06:19:37 24 4
gpt4 key购买 nike

我要搜索一个数据库,条件是:

select * from table where colnam in ('1','2','3','4','5'//动态检索);

因此,如果任何字段不可用,我会得到这样的查询,例如,如果只有两个字段存在:

select * from table where colnam in ('1','2','','','');

但我想要这样的查询

select * from table where colnam('1','2');

我不想要多余的引号和逗号。

$qry="select * from table where colnam in ('$int1','$int2','$int3','$int4','$int5')";

echo $qry;

返回

select * from table where colnam in ('1','2','','','');

如果 $int3$int4$int5 不存在

最佳答案

将非空值添加到数组中,并使用 , 对其进行内爆,并在 IN() 中使用它

$vals = array();
for($i=1;$i<=5;++$i){
if(isset(${"int".$i})) {
$x = ${"int".$i};
if(!empty($x))
array_push($vals,"'".${"int".$i}."'");
}
}

$ins = implode(",",$vals);

echo $qry="select * from table where colnam in ($ins)";

Demo

关于php - SQL查询中关键字内容的动态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33994741/

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