简体   繁体   中英

DBO Select where equals and like

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();}

According to this comment

When binding parameters, apparently you can't use a placeholder twice (eg "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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM