0

I'm currently having a bit of a problem I can't figure out and I guess I'm missing something.
This is the code that doesn't work (for some reason):

$search = "my search keyword";
$findTopics = $db->prepare('SELECT * FROM topics LEFT JOIN forum ON forum.f_id = topics.f_id WHERE topics.keywords LIKE "%:keyword%"');
$findTopics->bindValue(':keyword', $search);
$findTopics->execute();
var_dump($findTopics->fetchAll(PDO::FETCH_ASSOC));

The result when executing the above code is an empty array array(0) { }.
If you change :keyword to my search keyword and remove $findTopics->bindValue(..) it returns results. Also the query works fine if you execute it directly in mysql via a console.

What am I missing here?

1 Answer 1

4

Change the first two lines to:

$search = "%my search keyword%";
$findTopics = $db->prepare('SELECT * FROM topics LEFT JOIN forum ON forum.f_id = topics.f_id WHERE topics.keywords LIKE :keyword');

and it should work fine.

PDO will add the quotes around the parameters for you.

Sign up to request clarification or add additional context in comments.

1 Comment

Damn it.. I feel really stupid. Thanks. (I'll mark your answer in a couple of minutes)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.