Search code examples
phpmysqlwhere-clausesql-like

PHP QUERY still shows NOT results with LIKE?


I have a search that finds everything correctly, but my only problem now is that it also shows results I don't want to pull up. Query code below:

$query="SELECT * FROM users 
WHERE uid!='1' 
AND userf LIKE '%$key%' OR userl LIKE '%$key%' OR userm LIKE '%$key%'";

I want it to show the results it pulls up now, but not the ones where the code reads uid!='1'.


Solution

  • I think your problem here is the missing of brackets.

    $query="SELECT * FROM users WHERE uid != '1' AND ( userf LIKE '%$key%' OR userl LIKE '%$key%' OR userm LIKE '%$key%' )";
    

    Without the brackets the AND would only be between the first two conditions, all of the other ones would not be kicked out since they are OR'd

    Since the uid field is probably an INTEGER field, you should also not have the value 1 in quotes. It should be like this:

    $query="SELECT * FROM users WHERE uid != 1 AND ( userf LIKE '%$key%' OR userl LIKE '%$key%' OR userm LIKE '%$key%' )";