lovephp Posted November 16, 2012 Share Posted November 16, 2012 is it possible that i add another WHERE clause to this query?i want to add where user=$username ?? $results = mysql_query("SELECT * FROM table WHERE (`phone` LIKE '%".$query."%') OR (`name` LIKE '%".$query."%')") or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/270801-add-more-where-in-query/ Share on other sites More sharing options...
lovephp Posted November 16, 2012 Author Share Posted November 16, 2012 (edited) or is this right, oh and the user is stored in session so i want it to display results according to users name $results = mysql_query("SELECT * FROM table WHERE (`user= ".$user."') AND (`phone` LIKE '%".$query."%') OR (`name` LIKE '%".$query."%')") or die(mysql_error()); Edited November 16, 2012 by lovephp Quote Link to comment https://forums.phpfreaks.com/topic/270801-add-more-where-in-query/#findComment-1393048 Share on other sites More sharing options...
DavidAM Posted November 16, 2012 Share Posted November 16, 2012 You need to group the OR part together (if that is what you want) WHERE user = 'username' AND ( phone = 'phone' OR name = 'name' ) This requires the user to match and either the phone or name to match. Quote Link to comment https://forums.phpfreaks.com/topic/270801-add-more-where-in-query/#findComment-1393051 Share on other sites More sharing options...
lovephp Posted November 16, 2012 Author Share Posted November 16, 2012 so i do not need to use the LIKE clause to do the search? Quote Link to comment https://forums.phpfreaks.com/topic/270801-add-more-where-in-query/#findComment-1393061 Share on other sites More sharing options...
DavidAM Posted November 16, 2012 Share Posted November 16, 2012 You should use LIKE if you need LIKE. I was just showing the structure to use when you need an AND along with an OR. Since AND and OR have the same precedence, they are evaluated from left to right (or top to bottom). If you have: "Condition1 AND Condition2 OR Condition3" it evaluates differently than having: "Condition2 OR Condition3 AND Condition1". So you use the parenthesis to make sure the conditions are evaluated the way you want: "Condition1 AND (Condition2 OR Condition3)" (which is also evaluated differently from either of the other two). Quote Link to comment https://forums.phpfreaks.com/topic/270801-add-more-where-in-query/#findComment-1393103 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.