Jump to content

Is this possible?


HiImNew

Recommended Posts

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.

Link to comment
Share on other sites

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 by benanamen
Link to comment
Share on other sites

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
)


Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by Psycho
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.