<?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>PHP Architect blog &#187; database</title>
	<atom:link href="http://www.php-architect.com/blog/tag/database/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.php-architect.com/blog</link>
	<description>me, myself, and my code</description>
	<lastBuildDate>Wed, 14 Sep 2011 12:50:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Build MySQL insert Query from Array that contains fields and values</title>
		<link>http://www.php-architect.com/blog/2009/02/22/build-mysql-insert-query-from-array-that-contains-fields-and-values/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=build-mysql-insert-query-from-array-that-contains-fields-and-values</link>
		<comments>http://www.php-architect.com/blog/2009/02/22/build-mysql-insert-query-from-array-that-contains-fields-and-values/#comments</comments>
		<pubDate>Sun, 22 Feb 2009 12:30:50 +0000</pubDate>
		<dc:creator>aibrahim</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[query]]></category>
		<guid isPermaLink="false">http://www.php-architect.com/blog/?p=261</guid>
		<description><![CDATA[<a href="http://www.php-architect.com/blog/2009/02/22/build-mysql-insert-query-from-array-that-contains-fields-and-values/" title="Build MySQL insert Query from Array that contains fields and values"></a>The following function takes table name, and array where its keys are the table fields and it&#8217;s values are the values to insert in each field, it constructs the MySQL Query based on the input and executes it to insert &#8230;<p class="read-more"><a href="http://www.php-architect.com/blog/2009/02/22/build-mysql-insert-query-from-array-that-contains-fields-and-values/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://www.php-architect.com/blog/2009/02/22/build-mysql-insert-query-from-array-that-contains-fields-and-values/" title="Build MySQL insert Query from Array that contains fields and values"></a><iframe src="http://www.facebook.com/plugins/like.php?href=http://www.php-architect.com/blog/2009/02/22/build-mysql-insert-query-from-array-that-contains-fields-and-values/&amp;layout=standard&amp;show_faces=1&amp;width=450&amp;action=like&amp;colorscheme=light&amp;font=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:25px"></iframe><p>The following function takes table name, and array where its keys are the table fields and it&#8217;s values are the values to insert in each field, it constructs the MySQL Query based on the input and executes it to insert the data.</p>
<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> insertRow<span style="color: #009900;">&#40;</span><span style="color: #000088;">$table_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$input_array</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$link</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span>SERVER<span style="color: #339933;">,</span> USER<span style="color: #339933;">,</span> PASS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span>DATABASE<span style="color: #339933;">,</span> <span style="color: #000088;">$link</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$SQL</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO <span style="color: #006699; font-weight: bold;">$table_name</span> &quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$fields</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;(&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$values</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;(&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$input_array</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$k</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$fields</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;`<span style="color: #006699; font-weight: bold;">$k</span>` ,&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$values</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;'<span style="color: #006699; font-weight: bold;">$v</span>' ,&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000088;">$fields</span> <span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;#&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$values</span> <span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;#&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$fields</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,#&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fields</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$fields</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$fields</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$values</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,#&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$values</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$values</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$fields</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$values</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$SQL</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$fields</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; VALUES &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$values</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$SQL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">mysql_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$link</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.php-architect.com/blog/2009/02/22/build-mysql-insert-query-from-array-that-contains-fields-and-values/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

