首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mysql SELECT WHERE $VAR IN $VAR,$VAR,$VAR

Mysql SELECT WHERE $VAR IN $VAR,$VAR,$VAR
EN

Stack Overflow用户
提问于 2018-06-05 19:05:52
回答 1查看 75关注 0票数 0

有人知道如何使用字符串而不是数字来表示这条语句吗?

当$delivery=为“all”时,我尝试用$delivery拉取所有行,但我只能拉取1个单一变量,如果我想拉取一个数组,则我不能,因为我得到了数组转换为字符串的数值。

代码语言:javascript
复制
$delivery_con = [];
if ($delivery==="collection") { $delivery_con[]="no";}
if ($delivery==="delivery") {$delivery_con[]="yes";}
if ($delivery==="local") {$delivery_con[]="yes local";}

if ($delivery==="either") {$delivery_con=["no","yes","yes local"];}


$query="SELECT * 
        FROM testdata 
        WHERE title LIKE ? 
        AND location LIKE ? 
        AND postcode LIKE ? 
        AND price >=? 
        AND price <=? 
        AND cond=? 
        AND catagory LIKE ? 
        AND delivery IN ? 
        ORDER BY $order $dir";

$stat=$db->prepare($query);

$stat->execute(array("%$searchfor%",
                    "%$location%",
                    "%$postcode%",
                    "$pricefrom",
                    "$priceto",
                    "$cond",
                    "%$catagory%",
                    "$delivery_con"));

所以我的问题是,为了让select函数与$variables一起工作,

我真的卡住了。如果有人能帮上忙

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2018-06-05 19:44:18

在使用IN() SQL操作符时,您需要手动创建一组?并将其放入查询中:

代码语言:javascript
复制
<?php

$delivery_con = [];

if ($delivery === "collection") {
    $delivery_con[] = "no";
}
if ($delivery === "delivery") {
    $delivery_con[] = "yes";
}
if ($delivery === "local") {
    $delivery_con[] = "yes local";
}

if ($delivery==="either") {$delivery_con=["no","yes","yes local"];}


$in = str_repeat('?,', count($delivery_con) - 1) . '?';


$searchfor = "%$searchfor%";
$location  = "%$location%";
$postcode  = "%$postcode%";
$catagory  = "%$catagory%";

$array1 = array($searchfor,$location,$postcode,$pricefrom,$priceto,$cond,$catagory);

$params = array_merge($array1, $delivery_con);

$query = "SELECT * 
        FROM testdata 
        WHERE title LIKE ? 
        AND location LIKE ? 
        AND postcode LIKE ? 
        AND price >=? 
        AND price <=? 
        AND cond=? 
        AND catagory LIKE ? 
        AND delivery IN ($in) 
        ORDER BY $order $dir";

$stat = $db->prepare($query);

$stat->execute([$params]);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50698548

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档