PHP
1 public function getHotelsWithTags($word, $top, $tags) 2 { 3 try { 4 for ($i = 0; $i < count($tags); $i++) { 5 //tagsが1個の時 6 if (count($tags) == 1) { 7 $where = "'" . $tags[$i] . "'"; 8 break; 9 } 10 //tagsが2個以上の処理 11 if ($i == 0) { 12 //最初の処理 13 $where .= "'" . $tags[$i] . "'" . ","; 14 } elseif ($i + 1 == count($tags)) { 15 //最後の処理 16 $where .= "'" . $tags[$i] . "'"; 17 } else { 18 $where .= "'" . $tags[$i] . "'" . ","; 19 } 20 } 21 22 $sql = "SELECT hotels.hotel_id, hotels.country_code FROM hotels JOIN hotels_tags ON hotels.hotel_id = hotels_tags.hotel_id AND hotels.country_code = hotels_tags.country_code 23 JOIN tags ON hotels_tags.tags_id = tags.tags_id WHERE (hotels.hotel_id, hotels.country_code) in (SELECT hotel_id, country_code from areas where area like ?) 24 AND tags.tag in ({$where}) GROUP BY hotels.hotel_id, hotels.country_code HAVING count(*)=2 ORDER BY hotels.hotel_id LIMIT ?,5"; 25 26 $stmt = $this->db->prepare($sql); 27 $stmt->bindValue(1, "%{$word}%", PDO::PARAM_STR); 28 $stmt->bindValue(2, $top, PDO::PARAM_INT); 29 $stmt->execute(); 30 $results = $stmt ->fetchAll(PDO::FETCH_ASSOC); 31 return $results; 32 } catch (PDOException $e) { 33 return $e ->getMessage(); 34 } 35 }
という関数があります。コード自体は上手くいっています。ただ変数$sql内での({$where})の部分が未定義となっています。
書き方を間違えていますか。
回答1件
あなたの回答
tips
プレビュー