[PHP] Querying MySQL using 'AND' / 'OR'

2001-02-03 Thread James, Yz

Hi Guys,

Quick question..  I've managed to write a function (thanks to help from
people earlier in the week) which queries an admin table.

There are currently four fields in the table;  id, username, password, and
'site', where the 'site' means a subdirectory of my site that the particular
admin user is allowed acces to change things in

Now, each 'site' user is allowed access to only their site, but, being the
owner of all the 'sites', my field contains the word "all."  So, I used this
to determine whether the user (when logged in) is firstly an administrator,
and secondly what area of the site they're responsible for:

$sql = "SELECT * FROM table
WHERE username = '$username' AND password = '$password' AND site =
'theirsite' OR site = 'all'";

Now, as you can probably see, the table is querying whether the table has a
row with fields:

username, password, site OR

Just a row with the field containing "all."

So, without writing some if statements, is there any way of rewriting that
$sql check so that it checks whether the user is an admin of either the
individual subdirectory, or all of the sites?  Like

username: foo  password: bar site: all

or

username: foo  password: bar site: subdirectory

.  Hope that makes sense, and thanks in advance for any help :)

James.



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] Querying MySQL using 'AND' / 'OR'

2001-02-03 Thread Christian Reiniger

On Saturday 03 February 2001 15:02, James, Yz wrote:

 $sql = "SELECT * FROM table
 WHERE username = '$username' AND password = '$password' AND
 site = 'theirsite' OR site = 'all'";

 So, without writing some if statements, is there any way of rewriting
 that $sql check so that it checks whether the user is an admin of
 either the individual subdirectory, or all of the sites?  Like

 username: foo  password: bar site: all

 or

 username: foo  password: bar site: subdirectory

Simply use brackets to specify the evaluation order:

SELECT * FROM table
 WHERE (username = '$username' AND password = '$password') AND
 (site = 'theirsite' OR site = 'all')

-- 
Christian Reiniger
LGDC Webmaster (http://sunsite.dk/lgdc/)

"Never doubt that a small group of thoughtful, committed people can
change the world...
Indeed, it's the only thing that ever has."

- Margaret Mead

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]