Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay, here is a way to do it:</p> <p><strong>Edited, to fix bug when fetching multiple rows</strong></p> <pre><code>$sql = "SELECT `first_name`,`last_name` FROM `users` WHERE `country` =? AND `state`=?"; $params = array('Australia','Victoria'); /* In my real app the below code is wrapped up in a class But this is just for example's sake. You could easily throw it in a function or class */ // This will loop through params, and generate types. e.g. 'ss' $types = ''; foreach($params as $param) { if(is_int($param)) { $types .= 'i'; //integer } elseif (is_float($param)) { $types .= 'd'; //double } elseif (is_string($param)) { $types .= 's'; //string } else { $types .= 'b'; //blob and unknown } } array_unshift($params, $types); // Start stmt $query = $this-&gt;connection-&gt;stmt_init(); // $this-&gt;connection is the mysqli connection instance if($query-&gt;prepare($sql)) { // Bind Params call_user_func_array(array($query,'bind_param'),$params); $query-&gt;execute(); // Get metadata for field names $meta = $query-&gt;result_metadata(); // initialise some empty arrays $fields = $results = array(); // This is the tricky bit dynamically creating an array of variables to use // to bind the results while ($field = $meta-&gt;fetch_field()) { $var = $field-&gt;name; $$var = null; $fields[$var] = &amp;$$var; } $fieldCount = count($fieldNames); // Bind Results call_user_func_array(array($query,'bind_result'),$fields); $i=0; while ($query-&gt;fetch()){ for($l=0;$l&lt;$fieldCount;$l++) $results[$i][$fieldNames[$l]] = $fields[$fieldNames[$l]]; $i++; } $query-&gt;close(); // And now we have a beautiful // array of results, just like //fetch_assoc echo "&lt;pre&gt;"; print_r($results); echo "&lt;/pre&gt;"; } </code></pre>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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