title: How to bind mysqli bind_param arguments dynamically in PHP?
body: <p>I have been learning to use prepared and bound statements for my sql queries, and I have come out with this so far, it works ok but it is not dynamic at all when comes to multiple parameters or when there no parameter needed,</p>
  
  <pre><code>public function get_result($sql,$parameter)
      {
          # create a prepared statement
      $stmt = $this-&gt;mysqli-&gt;prepare($sql);
  
          # bind parameters for markers
      # but this is not dynamic enough...
          $stmt-&gt;bind_param("s", $parameter);
  
          # execute query 
          $stmt-&gt;execute();
  
      # these lines of code below return one dimentional array, similar to mysqli::fetch_assoc()
          $meta = $stmt-&gt;result_metadata(); 
  
          while ($field = $meta-&gt;fetch_field()) { 
              $var = $field-&gt;name; 
              $$var = null; 
              $parameters[$field-&gt;name] = &amp;$$var; 
          }
  
          call_user_func_array(array($stmt, 'bind_result'), $parameters); 
  
          while($stmt-&gt;fetch()) 
          { 
              return $parameters;
              //print_r($parameters);      
          }
  
  
          # close statement
          $stmt-&gt;close();
      }
  </code></pre>
  
  <p>This is how I call the object classes,</p>
  
  <pre><code>$mysqli = new database(DB_HOST,DB_USER,DB_PASS,DB_NAME);
  $output = new search($mysqli);
  </code></pre>
  
  <p>Sometimes I don't need to pass in any parameters,</p>
  
  <pre><code>$sql = "
  SELECT *
  FROM root_contacts_cfm
  ";
  
  print_r($output-&gt;get_result($sql));
  </code></pre>
  
  <p>Sometimes I need only one parameters,</p>
  
  <pre><code>$sql = "
  SELECT *
  FROM root_contacts_cfm
  WHERE root_contacts_cfm.cnt_id = ?
  ORDER BY cnt_id DESC
  ";
  
  print_r($output-&gt;get_result($sql,'1'));
  </code></pre>
  
  <p>Sometimes I need only more than one parameters,</p>
  
  <pre><code>$sql = "
  SELECT *
  FROM root_contacts_cfm
  WHERE root_contacts_cfm.cnt_id = ?
  AND root_contacts_cfm.cnt_firstname = ?
  ORDER BY cnt_id DESC
  ";
  
  print_r($output-&gt;get_result($sql,'1','Tk'));
  </code></pre>
  
  <p>So, I believe that this line is not dynamic enough for the dynamic tasks above,</p>
  
  <pre><code>$stmt-&gt;bind_param("s", $parameter);
  </code></pre>
  
  <p>To build a bind_param dynamically, I have found this on other posts online.</p>
  
  <pre><code>call_user_func_array(array(&amp;$stmt, 'bind_params'), $array_of_params);
  </code></pre>
  
  <p>And I tried to modify some code from <a href="http://php.net/manual/en/mysqli-stmt.bind-param.php" rel="noreferrer">php.net</a> but I am getting nowhere,</p>
  
  <pre><code>if (strnatcmp(phpversion(),'5.3') &gt;= 0) //Reference is required for PHP 5.3+ 
      { 
          $refs = array(); 
          foreach($arr as $key =&gt; $value) 
              $array_of_param[$key] = &amp;$arr[$key]; 
  
         call_user_func_array(array(&amp;$stmt, 'bind_params'), $array_of_params);
  
       }
  </code></pre>
  
  <p>Why? Any ideas how I can make it work?</p>
  
  <p>Or maybe there are better solutions?</p>