任何人都可以看到我如何使用String而不是Numeral用于此语句,
我试图在$ 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一起使用,
我真的被困住了。如果有人可以提供帮助
谢谢。
答案 0 :(得分:0)
使用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]);