HiImNew Posted August 24, 2016 Share Posted August 24, 2016 Is it possible to use multiple WHERE clauses to find values? Such as: $sql = mysqli_query($connection, "SELECT `user` FROM `chat_users` WHERE ( (`access` = '0') AS `test`, (`access` = '1') AS `test2`, (`access` = '2') AS `test3` )"); I'm just trying to generate a list of users based on their access level in one query if possible. Not too familiar with joins if that is what this requires. Quote Link to comment https://forums.phpfreaks.com/topic/301977-is-this-possible/ Share on other sites More sharing options...
benanamen Posted August 24, 2016 Share Posted August 24, 2016 (edited) You are looking for the OR operator. https://www.techonthenet.com/mysql/or.php Edited August 24, 2016 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/301977-is-this-possible/#findComment-1536520 Share on other sites More sharing options...
HiImNew Posted August 24, 2016 Author Share Posted August 24, 2016 I've read the page but I'm not sure how that would work? I want to get a list of all 3 values, not just one. Quote Link to comment https://forums.phpfreaks.com/topic/301977-is-this-possible/#findComment-1536521 Share on other sites More sharing options...
benanamen Posted August 24, 2016 Share Posted August 24, 2016 (edited) I can't really make it any simpler than it is. Did you even try the examples on the page? SELECT something FROM somewhere WHERE this OR that OR that OR that OR that_too. You spent no more than 5 minutes on it from the time you replied back. Edited August 24, 2016 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/301977-is-this-possible/#findComment-1536522 Share on other sites More sharing options...
HiImNew Posted August 24, 2016 Author Share Posted August 24, 2016 Yes I did. $sql = mysqli_query($connection, "SELECT `user` FROM `chat_users` WHERE `access` = '0' OR `access` = '1' OR `access` = '2' "); while($row = mysqli_fetch_array($sql)) { error_log(print_r($row, true)); } [23-Aug-2016 21:29:44 America/New_York] Array ( [0] => Admin [user] => Admin ) Quote Link to comment https://forums.phpfreaks.com/topic/301977-is-this-possible/#findComment-1536526 Share on other sites More sharing options...
Jacques1 Posted August 24, 2016 Share Posted August 24, 2016 So? What's the problem? If you're not getting the rows you've expected, there's something wrong with your access column. What's odd is that you're using numeric strings instead of actual numbers. Why? In any case, double-check that the access column in your table indeed has the exact values '0', '1' or '2'. If the column type is (VAR)CHAR or TEXT, there may be spaces or unprintable characters hidden in the strings. The best solution is to switch to a actual integers. Quote Link to comment https://forums.phpfreaks.com/topic/301977-is-this-possible/#findComment-1536545 Share on other sites More sharing options...
Psycho Posted August 24, 2016 Share Posted August 24, 2016 (edited) There's another solution as well. Not that it will solve your current problem since the above solution is valid. As the other two stated, that problem is likely in your data. Anyway, the other solution is to use the IN operator. This is useful when attempting to find matching values against the same field WHERE `access` IN (1, 2, 3) That assumes the field has actual numbers and not text stored as number. If the latter, then the values should be included in quotes as you've been doing. WHERE `access` IN ('1', '2', '3') Or, if the field is numbers (as it should be) and the values represent increasing (or decreasing) levels of access, it would seem the values would always be 1, 2, 3, . . . and increasing value. In that case you could use a simple less than (or less than and equal) operator WHERE `access` <= 3 Edited August 24, 2016 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/301977-is-this-possible/#findComment-1536547 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.