Php Mysql选择$VAR在$VAR、$VAR、$VAR中的位置

Php Mysql选择$VAR在$VAR、$VAR、$VAR中的位置,php,mysql,select,Php,Mysql,Select,有人知道我怎么用字符串来代替数字吗 当$delivery=“all”时,我试图用$delivery提取所有行,但我只能提取一个变量,如果我想提取数组,我不能,因为我将数组转换为字符串 $delivery_con = []; if ($delivery==="collection") { $delivery_con[]="no";} if ($delivery==="delivery") {$delivery_con[]="yes";} if ($delivery==="local") {$del

有人知道我怎么用字符串来代替数字吗

当$delivery=“all”时,我试图用$delivery提取所有行,但我只能提取一个变量,如果我想提取数组,我不能,因为我将数组转换为字符串

$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"));
$delivery\u con=[];
如果($delivery==“collection”){$delivery\u con[]=“no”}
如果($delivery==“delivery”){$delivery\u con[]=“yes”}
如果($delivery==“local”){$delivery\u con[]=“yes local”}
如果($delivery==“任意”){$delivery_con=[“否”、“是”、“是本地”];}
$query=“选择*
从测试数据
你喜欢什么?
位置怎么样?
邮政编码怎么样?
价格>=?
使用IN()SQL运算符时,需要手动创建一组
,并将它们放入查询中:

<?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]);

而不是使用占位符
是否尝试将变量直接放入语句中,如:
{$delivery\u con}
?为什么
$delivery\u con
需要是一个数组?您必须编写一小段代码,将数组转换为字符串,该字符串的格式适合您选择的
运算符中的
。查看语法要求,然后从数组上的循环构建字符串。如下面指出的@MCMXCII。这可能是帮助:。好的,谢谢你,我会查看你的信息,谢谢需要是一个数组,因为在数据库中,我有是,否,是本地的,如果用户选择查看所有行,我需要列出其中任何一行…但是station将只提取所选的变量,而不是全部3。我必须进行单独的查询以使其工作king是$delivery=“other”/query2-但是iv也有其他具有相同问题的表单要排序,所以我需要一个station,如果选择edif($delivery==“other”){$delivery\u con=“no,yes,yes local”}$query=“SELECT*FROM testdata WHERE title LIKE,它将拉取所有行?位置怎么样?邮政编码怎么样?价格>=?和price,但它不会显示[“否,是,是本地”]的所有行,仅显示其他行……您是否尝试在没有占位符的情况下运行查询?否,这是一个准备好的状态,我不确定是否可以,我会尝试。@Melissahick Ofcz您可以测试puporses吗