gpt4 book ai didi

php - 如何最小化mysql查询的if else条件?

转载 作者:行者123 更新时间:2023-11-29 07:36:34 25 4
gpt4 key购买 nike

需要:

当条件为 false 时,我需要使用 MySql 查询选择表字段中的所有值。

下面我只显示了一些代码,因为数据库字段很大。

代码:

<?php 
include "db.php";
$type1=$_POST['type'];
if($type1=='all')
{
$type='*';
}
else
{
$type=$type1;
}
$selecttype=mysqli_query($con, "SELECT * FROM product where type='$type'");
?>

我知道*不正确。例如,仅使用 i 来选择全部。

但是当我使用上面的代码时查询不成功。这个问题有什么解决办法吗?

我的完整代码:

    <?php

$sort=$_POST['sort'];
$city=$_POST['city'];
$location=$_POST['location'];
$type=$_POST['type'];
$res_type=$_POST['res_type'];
$cus_type=$_POST['cus_type'];

$i=0;
include "db.php";
if($sort=='name')
$sort_by='name';
else
$sort_by=null;


if($city!=null && $location!=null && $sort!=null)
{

if($type=='cod')
{
if($cus_type=='all')
{
$qy=mysqli_query($con, "SELECT * FROM add_res where cod='on' and type='$res_type' and city='$city' and location='$location' ORDER BY $sort_by ");
}
else
{
$qy=mysqli_query($con, "SELECT * FROM add_res where cod='on' and type='$res_type' and cusine_type REGEXP '$cus_type' and city='$city' and location='$location' ORDER BY $sort_by ");
}

}
else if($type=='pickup')
{
if($cus_type=='all')
{
$qy=mysqli_query($con, "SELECT * FROM add_res where pickup='on' and type='$res_type' and city='$city' and location='$location' ORDER BY $sort_by ");
}
else
{
$qy=mysqli_query($con, "SELECT * FROM add_res where pickup='on' and type='$res_type' and cusine_type REGEXP '$cus_type' and city='$city' and location='$location' ORDER BY $sort_by ");
}

}
else
{
if($cus_type=='all')
{
$qy=mysqli_query($con, "SELECT * FROM add_res where city='$city' and type='$res_type' and location='$location' ORDER BY $sort_by ");
}
else
{
$qy=mysqli_query($con, "SELECT * FROM add_res where city='$city' and type='$res_type' and cusine_type REGEXP '$cus_type' and location='$location' ORDER BY $sort_by ");
}

}
}
else if($city!=null && $location==null && $sort!=null)
{

if($type=='cod')
{
if($cus_type=='all')
{
$qy=mysqli_query($con, "SELECT * FROM add_res where cod='on' and type='$res_type' and city='$city' ORDER BY $sort_by ");
}
else
{
$qy=mysqli_query($con, "SELECT * FROM add_res where cod='on' and type='$res_type' and cusine_type REGEXP '$cus_type'and city='$city' ORDER BY $sort_by ");
}

}
else if($type=='pickup')
{
if($cus_type=='all')
{
$qy=mysqli_query($con, "SELECT * FROM add_res where pickup='on' and type='$res_type' and city='$city' ORDER BY $sort_by ");
}
else
{
$qy=mysqli_query($con, "SELECT * FROM add_res where pickup='on' and type='$res_type' and cusine_type REGEXP '$cus_type' and city='$city' ORDER BY $sort_by ");
}

}
else
{
if($cus_type=='all')
{
$qy=mysqli_query($con, "SELECT * FROM add_res where city='$city' and type='$res_type' ORDER BY $sort_by ");
}
else
{
$qy=mysqli_query($con, "SELECT * FROM add_res where city='$city' and type='$res_type' and cusine_type REGEXP '$cus_type' ORDER BY $sort_by ");
}

}
}


else if($city!=null && $location!=null && $sort==null)
{

if($type=='cod')
{
if($cus_type=='all')
{
$qy=mysqli_query($con, "SELECT * FROM add_res where cod='on' and city='$city' and location='$location' and type='$res_type' ");
}
else
{
$qy=mysqli_query($con, "SELECT * FROM add_res where cod='on' and city='$city' and location='$location' and cusine_type REGEXP '$cus_type' and type='$res_type' ");
}

}
else if($type=='pickup')
{
if($cus_type=='all')
{
$qy=mysqli_query($con, "SELECT * FROM add_res where pickup='on' and city='$city' and type='$res_type' and location='$location' ");
}
else
{
$qy=mysqli_query($con, "SELECT * FROM add_res where pickup='on' and city='$city' and type='$res_type' and cusine_type REGEXP '$cus_type' and location='$location' ");
}

}
else
{
if($cus_type=='all')
{
$qy=mysqli_query($con, "SELECT * FROM add_res where city='$city' and type='$res_type' and location='$location' ");
}
else
{
$qy=mysqli_query($con, "SELECT * FROM add_res where city='$city' and cusine_type REGEXP '$cus_type' and type='$res_type' and location='$location' ");
}

}
}

else if($city!=null && $location==null && $sort==null)
{

if($type=='cod')
{
if($cus_type=='all')
{
$qy=mysqli_query($con, "SELECT * FROM add_res where cod='on' and city='$city' and type='$res_type' ");
}
else
{
$qy=mysqli_query($con, "SELECT * FROM add_res where cod='on' and city='$city' and FIND_IN_SET('$cus_type', cusine_type) and type='$res_type' ");
}

}
else if($type=='pickup')
{
if($cus_type=='all')
{
$qy=mysqli_query($con, "SELECT * FROM add_res where pickup='on' and city='$city' and type='$res_type' ");
}
else
{
$qy=mysqli_query($con, "SELECT * FROM add_res where pickup='on' and city='$city' and cusine_type REGEXP '$cus_type' and type='$res_type' ");
}

}
else
{
if($cus_type=='all')
{
$qy=mysqli_query($con, "SELECT * FROM add_res where city='$city' and type='$res_type' ");
}
else
{
$qy=mysqli_query($con, "SELECT * FROM add_res where city='$city' and cusine_type REGEXP '$cus_type' and type='$res_type' ");
}

}
}
else if($city==null && $location==null && $sort==null)
{


if($type=='cod')
{
if($cus_type=='all')
{
$qy=mysqli_query($con, "SELECT * FROM add_res where cod='on' and type='$res_type' ");
}
else
{
$qy=mysqli_query($con, "SELECT * FROM add_res where cod='on' and cusine_type REGEXP '$cus_type' and type='$res_type' ");
}

}
else if($type=='pickup')
{
if($cus_type=='all')
{
$qy=mysqli_query($con, "SELECT * FROM add_res where pickup='on' and type='$res_type' ");
}
else
{
$qy=mysqli_query($con, "SELECT * FROM add_res where pickup='on' and cusine_type REGEXP '$cus_type' and type='$res_type' ");
}

}
else
{
if($cus_type=='all')
{
$qy=mysqli_query($con, "SELECT * FROM add_res where type='$res_type ");
}
else
{
$qy=mysqli_query($con, "SELECT * FROM add_res where type='$res_type' and cusine_type REGEXP '$cus_type' ");
}

}
}
else if($city==null && $location==null && $sort!=null)
{

if($type=='cod')
{
if($cus_type=='all')
{
$qy=mysqli_query($con, "SELECT * FROM add_res where cod='on' and type='$res_type' ORDER BY $sort_by ");
}
else
{
$qy=mysqli_query($con, "SELECT * FROM add_res where cod='on' and cusine_type REGEXP '$cus_type' and type='$res_type' ORDER BY $sort_by ");
}

}
else if($type=='pickup')
{
if($cus_type=='all')
{
$qy=mysqli_query($con, "SELECT * FROM add_res where pickup='on' and type='$res_type' ORDER BY $sort_by ");
}
else
{
$qy=mysqli_query($con, "SELECT * FROM add_res where pickup='on' and cusine_type REGEXP '$cus_type' and type='$res_type' ORDER BY $sort_by ");
}

}
else
{
if($cus_type=='all')
{
$qy=mysqli_query($con, "SELECT * FROM add_res where type='$res_type' ORDER BY $sort_by ");
}
else
{
$qy=mysqli_query($con, "SELECT * FROM add_res where type='$res_type' and cusine_type REGEXP '$cus_type' ORDER BY $sort_by ");
}

}
}
while($res=mysqli_fetch_array($qy))
{
$id=$res['id'];
$name=$res['name'];
$image=$res['logo'];
$desciption=$res['description'];
$cod=$res['cod'];
$pickup=$res['pickup'];
$min_order=$res['min_order'];
$i++;
?>
// Some HTML Code with PHP
<?php } ?>

最佳答案

将您的代码更改为如下所示:

<?php 
include "db.php";
$type1=$_POST['type'];

if($type1=='all')
$type="Where 1=1";
else
$type='Where type = "'.$type1.'"';

$selecttype = mysqli_query($con, "SELECT * FROM product ".$type);
?>

编辑
这也适用于多种条件,您所要做的就是将值附加到 $type 变量,如下所示:

if($type1=='all')
$type="Where 1=1";
else
$type='Where type = "'.$type1.'"';

if($somethingElse == "example")
$type .= ' AND somethingElse = "'.$somethingElse.'"';

关于php - 如何最小化mysql查询的if else条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30613562/

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