AND OR statement returning all results

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

AND OR statement returning all results

Post by jonnyfortis »

im glad it reads ok it must be what i am looking for. i will expand on this then post

i have a query below

Code: Select all

$newClassID = $_SESSION["newClassID"];
$newRegisterID = $_SESSION['newRegisterID'];

mysql_select_db($database_sdma, $sdma);
$query_rsAtt = "SELECT * FROM sd_attendance, sd_customers, sd_classes, sd_discipline, sd_new_register WHERE (sd_new_register.newRegID = '$newRegisterID' AND sd_attendance.attendance_discipline = sd_discipline.disciplineID AND sd_new_register.newClassID = sd_classes.classID AND sd_attendance.attendance_cust_id = sd_customers.custID AND sd_discipline.disciplineID = sd_classes.disciplineID AND sd_classes.classID = '$newClassID' AND sd_new_register.newClassID = '$newClassID') AND (sd_customers.lesson_per_week1 = '$newClassID' OR sd_customers.lesson_per_week2 = '$newClassID' OR sd_customers.lesson_per_week3 = '$newClassID' OR sd_customers.lesson_per_week4 = '$newClassID' OR sd_customers.lesson_per_week5 = '$newClassID' OR sd_customers.lesson_per_week6 = '$newClassID' OR sd_customers.lesson_per_week7 = '$newClassID') ";
$rsAtt = mysql_query($query_rsAtt, $sdma) or die(mysql_error());
$row_rsAtt = mysql_fetch_assoc($rsAtt);
$totalRows_rsAtt = mysql_num_rows($rsAtt);
it needs to display the records from the following

Code: Select all

(sd_new_register.newRegID = '$newRegisterID' AND sd_attendance.attendance_discipline = sd_discipline.disciplineID AND sd_new_register.newClassID = sd_classes.classID AND sd_attendance.attendance_cust_id = sd_customers.custID AND sd_discipline.disciplineID = sd_classes.disciplineID AND sd_classes.classID = '$newClassID' AND sd_new_register.newClassID = '$newClassID'
then choose one of the following

Code: Select all

sd_customers.lesson_per_week1 = '$newClassID' OR sd_customers.lesson_per_week2 = '$newClassID' OR sd_customers.lesson_per_week3 = '$newClassID' OR sd_customers.lesson_per_week4 = '$newClassID' OR sd_customers.lesson_per_week5 = '$newClassID' OR sd_customers.lesson_per_week6 = '$newClassID' OR sd_customers.lesson_per_week7 = '$newClassID'
currently it is display all the custID

have i got the brackets in the wrong place?
Last edited by jonnyfortis on Tue Apr 19, 2016 7:13 am, edited 1 time in total.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: AND OR statement returning all results

Post by Celauran »

The query itself reads OK. Everything in the first paren group must be true and anything in the second group must be true. Without knowing the data or the domain, it's tough to say beyond that.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: AND OR statement returning all results

Post by Christopher »

Logically you don't need parens around the ANDs -- only around the ORs. If you remove the OR'd conditions, so you get back the base set of records you need before the lesson_per_week1-7 conditions?
(#10850)
Post Reply