0

Let say if i've database row settings [id,user,pass,sos]

I've the following mysql statement

$username and $password could be anything whatever

$query = mysql_query ("SELECT * FROM `settings` WHERE user='$username' AND pass='$password'")

i want to say

SELECT * FROM `settings` WHERE user='$username' AND pass='$password' or sos=$username and sos=$password

so my question is how to use or within select statement

like i wanna say

user='$username'
pass='$password'
or
sos = both $username and $password

Thanks for helping

Reham Fahmy
  • 4,803
  • 15
  • 48
  • 70

6 Answers6

3

You need to use some brackets to make sure you are correctly matching on related username/password pairs:

SELECT * 
FROM `settings` 
WHERE (user='$username' AND pass='$password') 
    or (sos='$username' and sos='$password')

However, you really need to use parameterized queries as the above is subject to SQL injection attack. See here for some examples on how to do this:

How can I prevent SQL injection in PHP?

Community
  • 1
  • 1
D'Arcy Rittich
  • 160,735
  • 37
  • 279
  • 278
1

You could do

SELECT * 
FROM `settings` 
WHERE (user='$username' AND pass='$password') or (sos='$username' and sos='$password')
Nicola Peluchetti
  • 74,514
  • 30
  • 136
  • 188
1

You just need some parenthetical groups. I added single quotes in the second group, where you were initially missing them.

SELECT * 
FROM `settings`
WHERE 
  (user='$username' AND pass='$password')
  OR (sos='$username' AND sos='$password')
Michael Berkowski
  • 260,803
  • 45
  • 432
  • 377
1

Use parentheses:

SELECT * 
FROM `settings` 
WHERE 
   (user='$username' AND pass='$password')
   OR
   (sos='$username' AND sos='$password')
bfavaretto
  • 70,503
  • 15
  • 107
  • 148
1

I think you need parenthesis

SELECT * FROM `settings` WHERE (user='$username' AND pass='$password') or (sos=$username and sos=$password)
Gus
  • 6,420
  • 6
  • 36
  • 54
1

Does it not work exactly like that? I would write

WHERE (user = '$username' AND pass = '$password')
OR ('$username' = '$password' AND sos = '$username');
Borodin
  • 125,056
  • 9
  • 69
  • 143