I’m trying to make a search query but I got it wrong
Here is my error that I got:
Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘: search’ at line 1
And here is my code:
public function search_cafe($search){ $sql = "SELECT * FROM cafe WHERE nama_cafe LIKE :search"; $query = $this -> pdo -> query($sql); $query -> execute(array(':search' => '%'. $search . '%')); return $query -> fetchAll(PDO::FETCH_ASSOC); }
Advertisement
Answer
Please try this hope this may resolve your issue.
public function search_cafe($search){ $query = $this->pdo->prepare('SELECT * FROM cafe WHERE nama_cafe LIKE ?'); $query->execute(["%$search%"]); while ($results = $query->fetch()) { echo $results['nama_cafe']; } }
Thanks