PHP
1 public function searchhotels($word) 2 { 3 $sql = "select hotel_id, country_code from areas where area like ?"; 4 $stmt = $this->db->prepare($sql); 5 /*❶*/$stmt->bindParam(1, "'%" . $word . "%'", PDO::PARAM_STR); 6 // ❷$stmt->bindValue(1, "'%{$word}%'", PDO::PARAM_STR); 7 $stmt->execute(); 8 }
という関数なのですがどこが間違っておりますか。
select hotel_id, country_code from areas where area like '%地名%';
これでをMySQLで動かすと値がきちんと取得できます。
❷の文を実行するとNULLが返ってきますが画面は表示されます。
❶を実行すると
Fatal error: Uncaught Error: Cannot pass parameter 2 by reference in /Applications/MAMP/htdocs/BookingSite/Database.php:32 Stack trace: #0 /Applications/MAMP/htdocs/BookingSite/showhotels.php(12): Database->searchhotels('\xE3\x82\xBB\xE3\x83\x96') #1 {main} thrown in /Applications/MAMP/htdocs/BookingSite/Database.php on line 32
となります。間違いを教えてください
変数で入力するとあった為、
PHP
1 public function searchhotels($word) 2 { 3 $sql = "select hotel_id, country_code from areas where area like ?"; 4 $searchword = "'%{$word}%'"; 5 $stmt = $this->db->prepare($sql); 6 $stmt->bindParam(1, $searchword, PDO::PARAM_STR); 7 // $stmt->bindValue(1, "'%{$word}%'", PDO::PARAM_STR); 8 $stmt->execute(); 9 } 10
のように変更しましたが、NULLが返ってきます。
回答3件
あなたの回答
tips
プレビュー