Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here goes the solution or the actual problem of binding a variable number of values to mysqli prepared statement: </p> <pre><code>&lt;?php $values = array('b','c','d'); $in = str_repeat("?,", count($values)); $in = trim($in, ","); $sql = "SELECT * from users where username in($in)"; $stm = $con-&gt;prepare($sql); $types = str_repeat("s", count($values)); if (strnatcmp(phpversion(),'5.3') &gt;= 0) { $bind = array(); foreach($values as $key =&gt; $val) { $bind[$key] = &amp;$values[$key]; } } else { $bind = $values; } array_unshift($bind, $types); call_user_func_array(array($stm, 'bind_param'), $bind); #var_dump($sql, $types, $bind, $con-&gt;error); $stm-&gt;execute(); $res = $stm-&gt;get_result(); while($row = $res-&gt;fetch_assoc()) var_dump($row); </code></pre> <p>Commented line is for development testing only. Extremely useful.</p> <p>But your initial problem was lack of error reporting.</p> <p>It spoiled you not only in this particular case but your whole experience with PHP.<br> Every time when something goes wrong, PHP will tell you - what happened and whom to blame. Only if you let it. And you always should. </p> <p>You may read this <a href="https://stackoverflow.com/a/15447204/285587">answer on error reporting basics</a></p> <p>While having an error message, you can simply google for it and find a solution in seconds. Or at least you will know, what certain problem you have. Which is actually behavior of <code>call_user_func_array()</code> function, that was suddenly changed.</p>
 

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