<tfoot id='Mc4F7'></tfoot>
    <bdo id='Mc4F7'></bdo><ul id='Mc4F7'></ul>

<i id='Mc4F7'><tr id='Mc4F7'><dt id='Mc4F7'><q id='Mc4F7'><span id='Mc4F7'><b id='Mc4F7'><form id='Mc4F7'><ins id='Mc4F7'></ins><ul id='Mc4F7'></ul><sub id='Mc4F7'></sub></form><legend id='Mc4F7'></legend><bdo id='Mc4F7'><pre id='Mc4F7'><center id='Mc4F7'></center></pre></bdo></b><th id='Mc4F7'></th></span></q></dt></tr></i><div id='Mc4F7'><tfoot id='Mc4F7'></tfoot><dl id='Mc4F7'><fieldset id='Mc4F7'></fieldset></dl></div>

  • <small id='Mc4F7'></small><noframes id='Mc4F7'>

      <legend id='Mc4F7'><style id='Mc4F7'><dir id='Mc4F7'><q id='Mc4F7'></q></dir></style></legend>

        如何创建动态 WHERE 子句

        时间:2023-07-30
          <tbody id='PLr9j'></tbody>
        <tfoot id='PLr9j'></tfoot>

        <i id='PLr9j'><tr id='PLr9j'><dt id='PLr9j'><q id='PLr9j'><span id='PLr9j'><b id='PLr9j'><form id='PLr9j'><ins id='PLr9j'></ins><ul id='PLr9j'></ul><sub id='PLr9j'></sub></form><legend id='PLr9j'></legend><bdo id='PLr9j'><pre id='PLr9j'><center id='PLr9j'></center></pre></bdo></b><th id='PLr9j'></th></span></q></dt></tr></i><div id='PLr9j'><tfoot id='PLr9j'></tfoot><dl id='PLr9j'><fieldset id='PLr9j'></fieldset></dl></div>

          • <small id='PLr9j'></small><noframes id='PLr9j'>

              <bdo id='PLr9j'></bdo><ul id='PLr9j'></ul>
                1. <legend id='PLr9j'><style id='PLr9j'><dir id='PLr9j'><q id='PLr9j'></q></dir></style></legend>
                  本文介绍了如何创建动态 WHERE 子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试创建一个动态 WHERE 子句,根据从下拉菜单中选择的选项,它将编译正确的 WHERE 子句.但我认为我做得不对.

                  I am trying to create a dynamic WHERE clause where depending on which options are chosen from the drop down menus, it will compile the correct WHERE clause. But I do not think I am doing it correctly.

                  首先应该有一个默认的 WHERE 子句,无论从下拉菜单中选择哪个选项,都应该有一个 WHERE 子句检查所选的 SessionId 所以这应该是 SessionId= ?

                  First of all there should be a default WHERE clause, no matter which option is selected from the drop down menus there should be a WHERE clause checking for selected SessionId so this should be SessionId = ?

                  然后根据从下拉菜单中选择的选项,它将编译 WHERE 子句中的其他字段.有两个下拉菜单分别用于StudentsQuestions.可能的结果是:

                  Then depending on the options chosen from the drop down menus it will compile the other fields in the WHERE clause. There are two drop down menus which are for Students and Questions. The possible outcomes are:

                  Student selected !='All' : 添加 StudentId = ?在 WHERE 子句中Student selected == 'All' : 删除 StudentId = ?从 WHERE 子句Question selected != 'All' : 添加 QuestionId = ?在 WHERE 子句中Question selected == 'All' : 删除 QuestionId = ?来自 WHERE 子句

                  Student selected != 'All' : Add StudentId = ? in WHERE clause Student selected == 'All' : Remove StudentId = ? from WHERE clause Question selected != 'All' : Add QuestionId = ? in WHERE clause Question selected == 'All' : Remove QuestionId = ? from WHERE clause

                  我的问题是如何设置?

                  以下是我目前拥有的:

                          if(isset($_POST['answerSubmit'])) // we have subbmited the third form
                          {
                  
                      $selectedstudentanswerqry = "
                      SELECT
                      StudentAlias, StudentForename, StudentSurname, q.SessionId, QuestionNo, QuestionContent, o.OptionType, q.NoofAnswers, GROUP_CONCAT( DISTINCT Answer
                      ORDER BY Answer SEPARATOR ',' ) AS Answer, r.ReplyType, QuestionMarks, 
                      GROUP_CONCAT(DISTINCT StudentAnswer ORDER BY StudentAnswer SEPARATOR ',') AS StudentAnswer, ResponseTime, MouseClick, StudentMark
                      FROM Student s
                      INNER JOIN Student_Answer sa ON (s.StudentId = sa.StudentId)
                      INNER JOIN Student_Response sr ON (sa.StudentId = sr.StudentId)
                      INNER JOIN Question q ON (sa.QuestionId = q.QuestionId)
                      INNER JOIN Answer an ON q.QuestionId = an.QuestionId
                      LEFT JOIN Reply r ON q.ReplyId = r.ReplyId
                      LEFT JOIN Option_Table o ON q.OptionId = o.OptionId
                      ";
                  
                      if ($_POST['student'] != 'All'){
                      $selectedstudentanswerqry .= "
                      WHERE (SessionId = ? AND StudentId = ?)
                      ";
                      }
                  
                      if ($_POST['question'] != 'All'){
                      $selectedstudentanswerqry .= "
                      WHERE (SessionId = ? AND QuestionId = ?)
                      ";
                      }
                  
                      $selectedstudentanswerqry .= "
                      GROUP BY sa.StudentId, q.QuestionId
                      ORDER BY StudentAlias, q.SessionId, QuestionNo
                      ";
                  
                      global $mysqli;
                      $selectedstudentanswerstmt=$mysqli->prepare($selectedstudentanswerqry);
                      if ($_POST['student'] != 'All'){
                      // You only need to call bind_param once
                      $selectedstudentanswerstmt->bind_param("ii",$_POST["session"],$_POST["student"]);
                      }
                      if ($_POST['question'] != 'All'){
                      // You only need to call bind_param once
                      $selectedstudentanswerstmt->bind_param("ii",$_POST["session"],$_POST["question"]);
                      }
                      // get result and assign variables (prefix with db)
                      $selectedstudentanswerstmt->execute(); 
                      $selectedstudentanswerstmt->bind_result($detailsStudentAlias,$detailsStudentForename,$detailsStudentSurname,$detailsSessionId,$detailsQuestionNo, 
                      $detailsQuestonContent,$detailsOptionType,$detailsNoofAnswers,$detailsAnswer,$detailsReplyType,$detailsQuestionMarks,$detailsStudentAnswer,$detailsResponseTime,
                      $detailsMouseClick,$detailsStudentMark);
                      $selectedstudentanswerstmt->store_result();
                      $selectedstudentanswernum = $selectedstudentanswerstmt->num_rows();     
                  
                      }
                  
                  
                      ?>
                  

                  推荐答案

                  尝试构建 where 数组

                  Try to build where array

                  $where = array();
                  
                  
                  if ((int) $studentID >0) {
                     $where[] = " studentID = '{$studentID}' ";
                  }
                  if ((int) $QuestionId  >0) {
                     $where[] = " QuestionId  = '{$QuestionId }' ";
                  }
                  

                  最后用 AND 语句将 $where 内爆

                  and at end implode $where by AND statment

                  if (!empty($where)) 
                  $query['where'] = ' WHERE '. implode(' AND ', $where);
                  

                  这只是一种方式.我没有调试这段代码.

                  It's only a way. I did not debug this code.

                  这篇关于如何创建动态 WHERE 子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:有没有办法打印 mysqli->execute() 所做的实际查询 下一篇:警告:mysqli_stmt::bind_result():绑定变量的数量与准备

                  相关文章

                  最新文章

                  • <bdo id='A70nT'></bdo><ul id='A70nT'></ul>
                  <tfoot id='A70nT'></tfoot>
                2. <i id='A70nT'><tr id='A70nT'><dt id='A70nT'><q id='A70nT'><span id='A70nT'><b id='A70nT'><form id='A70nT'><ins id='A70nT'></ins><ul id='A70nT'></ul><sub id='A70nT'></sub></form><legend id='A70nT'></legend><bdo id='A70nT'><pre id='A70nT'><center id='A70nT'></center></pre></bdo></b><th id='A70nT'></th></span></q></dt></tr></i><div id='A70nT'><tfoot id='A70nT'></tfoot><dl id='A70nT'><fieldset id='A70nT'></fieldset></dl></div>
                  <legend id='A70nT'><style id='A70nT'><dir id='A70nT'><q id='A70nT'></q></dir></style></legend>
                3. <small id='A70nT'></small><noframes id='A70nT'>