I am trying to obtain results for a given member where status is pending or accepted doing the below:
$status1 = "Pending";
$status2 = "Attended";
$query = $conn->prepare('SELECT * FROM members WHERE member_id=:mID AND status=:status1 OR status=:status2');
$query->execute(array(':mID' => $mID,':status1' => $status1, ':status2' => $status2));
if ($query->rowCount() > 0) {
//start to create my table
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
//create variable, loop through and fill the table etc
}
}else{
echo "something";
}
This displays data - however, it even obtains results not specific to the member id (mID). Meaning other members data too!
I'm clearly missing something and or my query is wrong but struggling to find anything..
Any help appreciated.
orstatements. or use aninlikeWHERE member_id=:mID AND status in (:status1, :status2)SELECT * FROM members WHERE member_id=:mID AND status='Pending' OR status='Attended', which would get rid of the execute line of code, and shortenpreparetoquery. Keeping theprepare()does have a negative consequence however, and that is that the query will hit your database 2 times instead of just once (This is a probably a micro-optimization unless you do this frequently). The only time prepare is needed, is if there is user input going to the database.