Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9198397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:16:20+00:00 2026-06-17T22:16:20+00:00

I want to use php to set dynamic WHERE clause by storing details in

  • 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:16:21+00:00Added an answer on June 17, 2026 at 10:16 pm

    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));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use the sharer.php popup to open the share dialog, but would
I want to use an associative array with the PHP iterator: http://php.net/manual/en/class.iterator.php is it
I have a very basic validation script using PHP, and I want to set
there is a function in php to set default timezone- date_default_timezone_set('Asia/Calcutta'); but does this
Sorry my Gods, but.... I have php, file, and in this php i set
I want to use PHP to replace javascript functions in HTML documents. For example:
I am currently using various back-end services and I want to use PHP to
Simple question I guess, I want to use PHP to write an update to
I want use a single php file to handle all of my voting requests.
I use the PHP language for file upload and want to know that the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.