This question already has an answer here:
- pdo prepared statements with wildcards 2 answers
I'm trying to query posts using PDO where the database column 'tags' = something.
My problem is: I want my query to work even if there's no $_GET['tag']
request is set and here's my code.
if (!isset($_GET['tag'])) {
$tags = '%';
} else {
$tags = $_GET['tag'];
}
$get_recipes = $con->prepare ("SELECT * FROM recipes WHERE tags = ?");
$get_recipes->execute(array($tags));
$recipes = $get_recipes->fetchAll();
Is it valid to set the PHP variable $tags
to the MySQL wildcard %
? if not possible then what should I do to make my query work?
When I run that code and there's not $_GET['tag']
is written the query will not fetch any posts from the database.
</div>