php - DBO Select where equals and like

348

I have tried writing the code below 100 different ways and cannot figure out the proper syntax. It seems no matter how I write this, the $loggeduser is not passed to the WHERE username. What am I doing wrong? What is the proper syntax?

$query = $db->prepare('SELECT * FROM sites WHERE username = ? AND name LIKE ? OR login LIKE ? AND category="te"');
$query->bindValue(1, $loggeduser);
$query->bindValue(2, '%'.$_POST[search].'%');
$query->bindValue(3, '%'.$_POST[search].'%');
$query->execute();}
203

Answer

Solution:

According to this comment

When binding parameters, apparently you can't use a placeholder twice (e.g. "select * from mails where sender=:me or recipient=:me"), you'll have to give them different names otherwise your query will return empty handed (but not fail, unfortunately).

This may be your issue, as you're attempting to bind the same value to two separate placeholders. Give the following a try:

$query = $db->prepare('SELECT * FROM sites WHERE username = :username AND name LIKE :name OR login LIKE :login AND category="te"');
$query->bindValue(:username, $loggeduser);
$query->bindValue(:name, '%'.$_POST['search'].'%');
$query->bindValue(:login, '%'.$_POST['search'].'%');
$query->execute();

People are also looking for solutions to the problem: php - Magento curl error 503 after ~60 seconds

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.