Reputation: 322
I need to know how to query
a mysql database
based on multiple and conditions...
im trying to find all male users online above age 18.
php code :
$sql = mysql_query("Select * FROM `users` where `round`= '".$userData["round"]."',
`sex` = 'm', `age` = '18', `online_stas` = 'online'
")or die(mysql_error());
Upvotes: 1
Views: 4137
Reputation: 1335
Reading responses and the fact that you still have errors, I come to think the posibility of having "M" instead of "m" in sex
field.
You can try:
$sql = mysql_query("SELECT * FROM `users`
WHERE UPPER(`sex`) = 'M'
AND `age` > 18
AND `online_stas` = 'online'")
or die(mysql_error());
I removed the round check based on your statement that you want all male users online being older than 18, but can be added again.
Other posible fail can be the comparison of age
with string value 18 instead of numeric value 18, so I changed that too (despite it may work and be converted automatically, but better to check with same types if something changes in a future).
Try it and tell if it worked.
Upvotes: 2
Reputation:
You need to use AND
when specifying multiple conditions.
$sql = mysql_query("SELECT * FROM `users` WHERE `round`= '". mysql_escape_string($userData["round"])."' AND `sex` = 'm' AND `age` = '18' AND `online_stas` = 'online'") or die(mysql_error());
Also, for security pruproses, you may want to escape queries parameters with mysql_escape_string().
Upvotes: 0
Reputation: 16510
You'll want to use WHERE
and AND
, ending up with the following conditions:
"... WHERE `round`= '".$userData["round"]."' AND `sex` = 'm' AND `age` > 18 AND `online_stas` = 'online'"
Upvotes: 0
Reputation: 14794
Try using AND
.
$sql = mysql_query("Select * FROM `users` where `round`= '".$userData["round"]."' AND
`sex` = 'm' AND `age` >= '18' AND `online_stas` = 'online'
")or die(mysql_error());
Also, I changed your age
check to use >=
instead of =
, due to your description stating you want all users above age 18, not equal to. I'm also assuming you want to include those who are 18 as well as those above, if not, change it to >
.
Upvotes: 1