0

I want to use php to set dynamic WHERE clause by storing details in an array. But I want to have a default WHERE where it has to check for SchoolId = ?, no matter which option is chosen. My question is where do I store the default WHERE for SchoolId = ? Best to put it straight in the $query or put it in the $where array?

$query = 'SELECT ... FROM ...';

// Initially empty
$where = array();
$parameters = array();

// Check whether a specific student was selected
if($stu !== 'All') {
    $where[] = 'stu = ?';
    $parameters[] = $stu;
}

// Check whether a specific question was selected
// NB: This is not an else if!
if($ques !== 'All') {
    $where[] = 'ques = ?';
    $parameters[] = $ques;
}

// If we added to $where in any of the conditionals, we need a WHERE clause in
// our query
if(!empty($where)) {
    $query .= ' WHERE ' . implode(' AND ', $where);
}

Also to set up the bind_param(), what is the correct set up to include this? Do I need to set them up in the if statements above or include seperate if statements?

Below are the bind params:

$selectedstudentanswerstmt=$mysqli->prepare($selectedstudentanswerqry);
// You only need to call bind_param once
$selectedstudentanswerstmt->bind_param("iii",$_POST["school"],$_POST["student"],$_POST["question"]); 

//$_POST["school"] -- SchoolId parameters
//$_POST["student"] -- StudentId parameters
//$_POST["question"] -- QuestionId parameters
2
  • this is 4rth question in a row Commented Jan 27, 2013 at 14:43
  • I got the idea from that question but that is not my question. That is somebody else's? Probably somebody is doing the same work Commented Jan 27, 2013 at 14:59

1 Answer 1

1

My personal preference is to put the default in the $where array. That way if you ever need to debug or track the values you get a complete look at what is being put into the array.

As far as where to bind the params, you will need to do it after you prepare the query so you will need a second set of if statements after your query is constructed.

// Initially empty
$where = array('SchoolId = ?');
$parameters = array($schoolID);
$parameterTypes = 'i';

// Check whether a specific student was selected
if($stu !== 'All') {
    $where[] = 'stu = ?';
    $parameters[] = $stu;
    $parameterTypes .= 'i';
}

// Check whether a specific question was selected
// NB: This is not an else if!
if($ques !== 'All') {
    $where[] = 'ques = ?';
    $parameters[] = $ques;
    $parameterTypes .= 'i';
}

// If we added to $where in any of the conditionals, we need a WHERE clause in
// our query
if(!empty($where)) {
    $query .= ' WHERE ' . implode(' AND ', $where);
    $selectedstudentanswerstmt=$mysqli->prepare($query);
    // You only need to call bind_param once
    $selectedstudentanswerstmt->bind_param($parameterTypes,implode($parameters));
}
Sign up to request clarification or add additional context in comments.

3 Comments

so if you look at my code snippet, do I just put underneath $parameters = array(); ... $where[] = '$_POST['school'] = ?'; $parameters[] = $_POST['school']; to set the default up?
Can you show me the second set of if/else statements with comments stating which bind params should go in each. DO i need to just include the three if statements I have and do similar for second set of if/else statements for bind params?
I edited a code in my answer a tad. After looking at it again I thought it made sense to set the options in the "if" blocks already there. I was't sure the variable name of the school id, but I set it as a default in $parameters() at the top.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.