title: Dynamically Bind Params in Prepared Statements with MySQLi
body: <p>So like the title says, i've search here and tried almost everything, with no success.</p>
  
  <p>I've try to test something very simple before going more deep, to make sure it works. But even at its simplest, i always get 0 results and i know there are 67 results.</p>
  
  <p>What's wrong with my code?</p>
  
  <p>Thanks</p>
  
  <pre><code>$conn = connect(); // connect to the db
  
  $a_bind_params = array('love', 'circle');
  $a_param_type = array('s', 's');
  
  $totalKeywords = count($a_bind_params);
  
  $q = 'SELECT id, name
      FROM album
      WHERE name LIKE ?';
  
  for ($i = 1; $i &lt; $totalKeywords; $i++) {
      $q .= ' AND name LIKE ?';
  }
  
  echo $q; // for testing purposes: verify that query is OK
  
  // bind parameters.
  $param_type = '';
  $n = count($a_param_type);
  
  for($i = 0; $i &lt; $n; $i++) {
      $param_type .= $a_param_type[$i];
  }
  
  /* with call_user_func_array, array params must be passed by reference */
  $a_params = array();
  $a_params[] = &amp; $param_type;
  
  for($i = 0; $i &lt; $n; $i++) {
    /* with call_user_func_array, array params must be passed by reference */
    $a_params[] = &amp; $a_bind_params[$i];
  }
  
  $stmt = $conn-&gt;prepare($q);
  
  /* use call_user_func_array, as $stmt-&gt;bind_param('s', $param); does not accept params array */
  call_user_func_array(array($stmt, 'bind_param'), $a_params);
  
  
  $stmt-&gt;execute();
  $stmt-&gt;store_result();
  $num_rows = $stmt-&gt;num_rows;
  
  echo $num_rows; // how many found ?
  
  $stmt-&gt;bind_result($id, $name);
  
  while($stmt-&gt;fetch()) {
      echo $name;
  }
  
  $stmt-&gt;free_result();
  $stmt-&gt;close();
  $conn-&gt;close(); 
  </code></pre>