如何在列的where子句中使用implode'值

时间:2015-10-20 08:29:02

标签: php mysql arrays json

解码前的部分JSON字符串:,"avail":["Wed-2","Wed-3"]

已解码:$data = json_decode($return, true); 存储在变量中:$avail = $data['avail'];

//数组声明

$days = array();
$cols = array();

$avail:

以上的格式
     if($avail != ""){
        foreach($avail as $k=>$v)
        {
            echo $v;

            $array = explode('-', $v);
            $day =$array[0]; // Wed
            $column =  $array[1]; // 2

            if($column == 1)
            {
            $col = "morning";

            }
            if($column == 2)
            {
                $col = "afternoon";
            }
            if($column == 3)
            {
                $col = "evening";
            }
             echo $col ."=>". $day;

            array_push($cols,$col);
            array_push($days,$day);
        }
        }

//现在使用数组($ days)匹配列'上午'在帖子表中。

echo $sql=" SELECT * , (3956 * 2 * ASIN(SQRT( POWER(SIN(('$lat' - lat) *  pi()/180 / 2), 2) +COS('$lat' * pi()/180) * COS(lat * pi()/180) * POWER(SIN(('$lon' - lon) * pi()/180 / 2), 2) ))) as distance  
from posts,subjects WHERE posts.afternoon IN (" . implode(", ",$days) . ") AND posts.catID = '$catid' AND posts.subname LIKE '%$subject%' AND posts.subid = subjects.subid AND posts.catID = subjects.catid  AND posts.pricing <= '$rate'  having  distance <= '$distance' order by distance ";
echo"<br/>";
        $stmt =connection::$pdo->prepare($sql);
        $stmt->execute();
$place=array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
               $place[] = $row;
               }

var_dump($place);

但它返回了这个错误:

  

致命错误:未捕获的异常&#39; PDOException&#39;与消息   &#39; SQLSTATE [42S22]:未找到列:1054未知列&#39; Wed&#39;在   &#39; where where&#39;&#39;在第128行的C:\ wamp \ www \ fetch_tutor.php中(!)   PDOException:SQLSTATE [42S22]:找不到列:1054未知列   &#39;星期三&#39;在&#39; where子句&#39;在第128行的C:\ wamp \ www \ fetch_tutor.php

基于此,我理解提供的数组($ day)被视为字段名称。事实上,这是在早上检查的数组中的价值&#39;邮政表中的字段。

我该怎么做呢。我已经浪费了差不多一天的时间了!

1 个答案:

答案 0 :(得分:2)

应该是

WHERE posts.afternoon IN ('" . implode("',' ",$days) . "') 

获得类似WHERE posts.afternoon IN ('Mon','Wed','Sun')的内容。