Jump to content

Select statement with And & Or at the same time


IndianaGuy

Recommended Posts

I am getting incorrect results with this (getting future events in the results). I am not sure if it is a date issue, or the select statement is bad. I am trying to select all the rows that have $ses_memberID in any of those columns and at the same time, only display the rows that will be on the correct date. The table structure for pp_date is the standard Date type which i believe is Y-m-d format. My next step will be to find out which column is found the search criteria in. Just in case that will effect how this step will look. Thank you so much for your help!

	$todaysDate=date("Y-m-d");
	echo $todatsDate; // testing and the output is 2017-11-19
	
$sqlMC = "SELECT * FROM tbl_pp WHERE Col1 Or Col2 Or Col3 OR Col4 OrCol5 Or Col6 = '$ses_memberID' AND pp_date = '$todaysDate'";
	$resultMC = mysqli_query($conn, $sqlMC);
	      if (mysqli_num_rows($resultMC) > 0) {
               while($rowMC = mysqli_fetch_array($resultMC)) {
                     //printing the rows
               }
     }  else {
    echo "none found";
    }
	//OUTPUT IS : I get 2017-11-19 rows printed and also 2017-11-20 showing as well!!
	

 

Link to comment
Share on other sites

That list of ORs isn't going to do anything (or, more specifically, it's going to return every row where any of those 5 columns are not empty).  If you want to test if a certain value is in multiple columns you need to spell it out:

Col1 = '$ses_memberID' OR Col2 = '$ses_memberID' OR ...

You should also use parentheses to group the OR and AND operators to be explicit about what you're telling it to do.  Lastly, you should use prepared statements whenever you need to put data in a query.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...