note 109256 modified in mysqli-stmt.bind-param by aharvey

From: Date: Thu, 19 Jul 2012 00:59:59 +0000
Subject: note 109256 modified in mysqli-stmt.bind-param by aharvey
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-189293@lists.php.net to get a copy of this message
When dealing with a dynamic number of field values while preparing a statement I find this class useful.&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;class BindParam{&#13;&#10;&#9;private $values = array(), $types = &#39;&#39;;&#13;&#10;&#9;&#13;&#10;&#9;public function add( $type, &#38;$value ){&#13;&#10;&#9;&#9;$this-&#62;values[] = $value;&#13;&#10;&#9;&#9;$this-&#62;types .= $type;&#13;&#10;&#9;}&#13;&#10;&#9;&#13;&#10;&#9;public function get(){&#13;&#10;&#9;&#9;return array_merge(array($this-&#62;types), $this-&#62;values);&#13;&#10;&#9;}&#13;&#10;}&#13;&#10;?&#62;&#13;&#10;&#13;&#10;Usage is pretty simple. Create an instance and use the add method to populate. When you&#39;re ready to execute simply use the get method.&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;$bindParam = new BindParam();&#13;&#10;$qArray = array();&#13;&#10;&#13;&#10;$use_part_1 = 1;&#13;&#10;$use_part_2 = 1;&#13;&#10;$use_part_3 = 1;&#13;&#10;&#13;&#10;$query = &#39;SELECT * FROM users WHERE &#39;;&#13;&#10;if($use_part_1){&#13;&#10;&#9;$qArray[] = &#39;hair_color = ?&#39;;&#13;&#10;&#9;$bindParam-&#62;add(&#39;s&#39;, &#39;red&#39;);&#13;&#10;}&#13;&#10;if($use_part_2){&#13;&#10;&#9;$qArray[] = &#39;age = ?&#39;;&#13;&#10;&#9;$bindParam-&#62;add(&#39;i&#39;, 25);&#13;&#10;}&#13;&#10;if($use_part_3){&#13;&#10;&#9;$qArray[] = &#39;balance = ?&#39;;&#13;&#10;&#9;$bindParam-&#62;add(&#39;d&#39;, 50.00);&#13;&#10;}&#13;&#10;&#13;&#10;$query .= implode(&#39; OR &#39;, $qArray);&#13;&#10;&#13;&#10;//call_user_func_array( array($stm, &#39;bind_param&#39;), $bindParam-&#62;get());&#13;&#10;&#13;&#10;echo $query . &#39;&#60;br/&#62;&#39;;&#13;&#10;var_dump($bindParam-&#62;get());&#13;&#10;?&#62;&#13;&#10;&#13;&#10;This gets you the result that looks something like this:&#13;&#10;&#13;&#10;SELECT * FROM users WHERE hair_color = ? OR age = ? OR balance = ?&#13;&#10;array(4) { [0]=&#62; string(3) &#34;sid&#34; [1]=&#62; string(3) &#34;red&#34; [2]=&#62; int(25) [3]=&#62; float(50) }&#13;&#10;&#13;&#10;[Editor&#39;s note: changed BindParam::add() to accept $value by reference and thereby prevent a warning in newer versions of PHP.] --was-- When dealing with a dynamic number of field values while preparing a statement I find this class useful. <?php class BindParam{ private $values = array(), $types = ''; public function add( $type, $value ){ $this->values[] = $value; $this->types .= $type; } public function get(){ return array_merge(array($this->types), $this->values); } } ?> Usage is pretty simple. Create an instance and use the add method to populate. When you're ready to execute simply use the get method. <?php $bindParam = new BindParam(); $qArray = array(); $use_part_1 = 1; $use_part_2 = 1; $use_part_3 = 1; $query = 'SELECT * FROM users WHERE '; if($use_part_1){ $qArray[] = 'hair_color = ?'; $bindParam->add('s', 'red'); } if($use_part_2){ $qArray[] = 'age = ?'; $bindParam->add('i', 25); } if($use_part_3){ $qArray[] = 'balance = ?'; $bindParam->add('d', 50.00); } $query .= implode(' OR ', $qArray); //call_user_func_array( array($stm, 'bind_param'), $bindParam->get()); echo $query . '<br/>'; var_dump($bindParam->get()); ?> This gets you the result that looks something like this: SELECT * FROM users WHERE hair_color = ? OR age = ? OR balance = ? array(4) { [0]=> string(3) "sid" [1]=> string(3) "red" [2]=> int(25) [3]=> float(50) } http://php.net/manual/en/mysqli-stmt.bind-param.php

« previous php.notes (#189293) next »