Note that there are some explanatory texts on larger screens.

plurals
  1. POErrors appearing in mysqli code and call_user_func_array()
    primarykey
    data
    text
    <p>I am getting quite a few errors when trying to create a dynamic where clause using mysqli:</p> <blockquote> <p>Warning: Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given in ... on line 319</p> <p>Warning: mysqli_stmt::execute(): (HY000/2031): No data supplied for parameters in prepared statement in ... on line 328</p> <p>Warning: mysqli_stmt::bind_result(): (HY000/2031): No data supplied for parameters in prepared statement in ... on line 331</p> <p>Warning: mysqli_stmt::store_result(): (HY000/2014): Commands out of sync; you can't run this command now in ... on line 332</p> </blockquote> <p>Im guessing there is a little change that is needed to solve the problems but what happens is that if one of the two drop down menu's do not equal <code>All</code> or if both don't equal <code>All</code> then it comes up with the errors.</p> <p>Below is the code display both the drop down menus and the query (with dynamic where clause) that follows depending n options selected:</p> <p><strong>HTML:</strong></p> <p>Student Drop down menu:</p> <pre><code>&lt;select name="student" id="studentsDrop"&gt; &lt;option value="All"&gt;All&lt;/option&gt; &lt;option value="11"&gt;John May&lt;/option&gt; &lt;option value="23"&gt;Chris Park&lt;/option&gt; &lt;/select&gt; </code></pre> <p>Question Number Drop down menu</p> <pre><code>&lt;select name="question" id="questionsDrop"&gt; &lt;option value="All"&gt;All&lt;/option&gt; &lt;option value="123"&gt;1&lt;/option&gt; &lt;option value="124"&gt;2&lt;/option&gt; &lt;option value="125"&gt;3&lt;/option&gt; &lt;/select&gt; </code></pre> <p><strong>PHP/MYSQLI:</strong></p> <pre><code> function StudentAnswers() { /*BELOW IS THE QUERY WHERE I AM TRYING TO RETRIEVE DATA DEPENDING ON THE ASSESSMENT CHOSEN AND THEN DEPENDING ON OPTIONS CHOSEN IN STUDENT AND QUESTION NUMBER DROP DOWN MENU */ $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 "; // Initially empty $where = array('q.SessionId = ?'); $parameters = array($_POST["session"]); $parameterTypes = 'i'; // Check whether a specific student was selected if($_POST["student"] !== 'All') { $where[] = 'sa.StudentId = ?'; $parameters[] =&amp; $_POST["student"]; $parameterTypes .= 'i'; } // Check whether a specific question was selected // NB: This is not an else if! if($_POST["question"] !== 'All') { $where[] = 'q.QuestionId = ?'; $parameters[] =&amp; $_POST["question"]; $parameterTypes .= 'i'; } // If we added to $where in any of the conditionals, we need a WHERE clause in // our query if(!empty($where)) { $selectedstudentanswerqry .= ' WHERE ' . implode(' AND ', $where); global $mysqli; $selectedstudentanswerstmt=$mysqli-&gt;prepare($selectedstudentanswerqry); // You only need to call bind_param once call_user_func_array(array($selectedstudentanswerstmt, 'bind_param'), array_merge(array($parameterTypes), $parameters)); //LINE 319 ERROR 1 } //Add group by and order by clause to query $selectedstudentanswerqry .= " GROUP BY sa.StudentId, q.QuestionId ORDER BY StudentAlias, q.SessionId, QuestionNo "; // get result and assign variables (prefix with db) $selectedstudentanswerstmt-&gt;execute(); //LINE 328 ERROR 2 //bind database fields $selectedstudentanswerstmt-&gt;bind_result($detailsStudentAlias,$detailsStudentForename,$detailsStudentSurname,$detailsSessionId,$detailsQuestionNo, $detailsQuestonContent,$detailsOptionType,$detailsNoofAnswers,$detailsAnswer,$detailsReplyType,$detailsQuestionMarks,$detailsStudentAnswer,$detailsResponseTime, $detailsMouseClick,$detailsStudentMark); //LINE 331 ERROR 3 //store results retrieved $selectedstudentanswerstmt-&gt;store_result(); //LINE 332 ERROR 4 //count number of rows retrieved $selectedstudentanswernum = $selectedstudentanswerstmt-&gt;num_rows(); //output query echo "$selectedstudentanswerqry"; } ?&gt; </code></pre> <p><strong>Here is a DEMO:</strong> <a href="http://helios.hud.ac.uk/u0867587/Mobile_app/Text30.php" rel="nofollow">DEMO</a></p> <p>In demo select an assessment from drop down menu and submit. You will see the two drop down menus. Keep them both set as <code>All</code> and submit, it will output query with no problems. No in one of the drop down menus, change <code>All</code> to a specific student or question, then submit. Now you will see the errors</p> <p><strong>VAR DUMP:</strong></p> <p>The result of the <code>var_dump(array_merge(array($parameterTypes), $parameters)));</code> when I chose session (assessment) with value <code>31</code>, student number value <code>40</code>, and question number value <code>81</code>, AND WHERE CLAUSE <code>WHERE q.SessionId = ? AND sa.StudentId = ? AND q.QuestionId = ?</code>:</p> <p>I am getting this output: <code>array(4) { [0]=&gt; string(3) "iii" [1]=&gt; string(2) "31" [2]=&gt; string(2) "40" [3]=&gt; string(2) "81" }</code> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload