<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>James Boelen &#187; PHP</title>
	<atom:link href="http://james.boelen.ca/category/programming/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://james.boelen.ca</link>
	<description>Software Developer</description>
	<lastBuildDate>Thu, 09 May 2013 22:02:35 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5</generator>
		<item>
		<title>Dynamic Parameter Binding with MySQLi</title>
		<link>http://james.boelen.ca/programming/php/dynamic-parameter-binding-with-mysqli/</link>
		<comments>http://james.boelen.ca/programming/php/dynamic-parameter-binding-with-mysqli/#comments</comments>
		<pubDate>Mon, 11 Oct 2010 01:46:37 +0000</pubDate>
		<dc:creator>James Boelen</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[MySQLi]]></category>

		<guid isPermaLink="false">http://james.boelen.ca/?p=6</guid>
		<description><![CDATA[A problem that I encountered recently was that I needed to bind a dynamic number of parameters to a mysql query in php. I'm using MySQLi to handle the connection and my queries.]]></description>
				<content:encoded><![CDATA[<p>A problem that I encountered recently was that I needed to bind a dynamic number of parameters to a mysql query in php. I&#8217;m using MySQLi to handle the connection and my queries.</p>
<p>The short answer to my problem was the <a href="http://ca.php.net/manual/en/function.call-user-func-array.php" target="_blank">call_user_func_array</a> function. Taken from the php.net documentation:</p>
<blockquote><p>Call a user function given with an array of parameters</p></blockquote>
<p>After toying around and looking at other examples I came up with the following piece of code.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$queryParams</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This was to be my array that would hold the parameter types and values.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$terms</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$t</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$queryTypes</span> <span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;s&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In my case, all of my parameters were strings. Creating a string with the parameter types was as easy as creating a string with the correct number of S&#8217;s.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$queryParams</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$queryTypes</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now I&#8217;ve added the string of parameter types to my array as the first value.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$terms</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$id</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$term</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>Opening my foreach loop to get all my parameters into the array</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$queryParams</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$terms</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>For each of my parameters, a reference is added to my array.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">call_user_func_array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'bind_param'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #000088;">$queryParams</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Finally, the magic. With my array now containing the parameter types and a reference to each of the parameters, I can call call_user_func_array. This sends my $queryParams array as inputs for bind_param on my $query variable (which I&#8217;d already bound a prepared MySQLi statement to).</p>
<p>Now, with my dynamic number of parameters bound, I can execute and fetch as per usual.</p>
]]></content:encoded>
			<wfw:commentRss>http://james.boelen.ca/programming/php/dynamic-parameter-binding-with-mysqli/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
