有人知道如何使用字符串而不是数字来表示这条语句吗?
当$delivery=为“all”时,我尝试用$delivery拉取所有行,但我只能拉取1个单一变量,如果我想拉取一个数组,则我不能,因为我得到了数组转换为字符串的数值。
$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一起工作,
我真的卡住了。如果有人能帮上忙
谢谢。
发布于 2018-06-05 19:44:18
在使用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]);https://stackoverflow.com/questions/50698548
复制相似问题