<?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>Vinothbabu&#039;s Desk! &#187; queries</title>
	<atom:link href="http://vinothbabu.com/tag/queries/feed/" rel="self" type="application/rss+xml" />
	<link>http://vinothbabu.com</link>
	<description>When Smart becomes Dumb!</description>
	<lastBuildDate>Thu, 01 Mar 2012 10:38:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>UPDATE and INSERT differences in syntax is an inconvenience</title>
		<link>http://vinothbabu.com/2010/05/08/update-and-insert-differences-in-syntax-is-an-inconvenience/</link>
		<comments>http://vinothbabu.com/2010/05/08/update-and-insert-differences-in-syntax-is-an-inconvenience/#comments</comments>
		<pubDate>Sat, 08 May 2010 17:23:34 +0000</pubDate>
		<dc:creator>Sachin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[mys]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[queries]]></category>

		<guid isPermaLink="false">http://vinothbabu.com/?p=288</guid>
		<description><![CDATA[An associative array is the conventional workaround of the coding world.. In an associative array, we can associate any key or index we want with each value. [codesyntax lang="php"] &#60;?php include('escape_arr.php'); //create an array $sets=array( 'userid'=&#62;$userid, 'mimetype'=&#62;$mimetype, 'bytesize'=&#62;$bytesize, 'filename'=&#62;$filename, 'extension'=&#62;$ext, 'lastmodified'=&#62;time(), 'height'=&#62;$height, 'width'=&#62;$width, 'binarydata'=&#62;$data ); list($sets,$cols,$values)=escape_arr($sets); $insert_sql="INSERT INTO `avatars` ".implode(',',$cols)." VALUES(".implode(',',$values).")"; $update_sql="UPDATE `avatars` SET ".implode(',',$sets)." [...]]]></description>
			<content:encoded><![CDATA[<p>An associative array is the conventional workaround of the coding world.. In an associative array, we can associate any key or index we want with each value.<span id="more-288"></span></p>
<p>[codesyntax lang="php"]</p>
<pre>&lt;?php

include('escape_arr.php');
//create an array
$sets=array(
'userid'=&gt;$userid,
'mimetype'=&gt;$mimetype,
'bytesize'=&gt;$bytesize,
'filename'=&gt;$filename,
'extension'=&gt;$ext,
'lastmodified'=&gt;time(),
'height'=&gt;$height,
'width'=&gt;$width,
'binarydata'=&gt;$data
);
list($sets,$cols,$values)=escape_arr($sets);
$insert_sql="INSERT INTO `avatars` ".implode(',',$cols)." VALUES(".implode(',',$values).")";
$update_sql="UPDATE `avatars` SET ".implode(',',$sets)." WHERE userid=$userid LIMIT 1";

if(!$avatar_exists)
{
$result=mysql_query($insert_sql) or die("Insert query failed!");
}else{
$result=mysql_query($update_sql) or die("Update query failed!");
}
echo 'Avatar updated!';
?&gt;
escape_arr.php
&lt;?php
function escape_arr($arr)
{
$out=array();
$sets=array();
foreach ($arr as $key =&gt; $value)
{
$k="`$key`";
$v="'".mysql_real_escape_string($value)."'";
$out[$k] = $v;
$sets[]="$k = $v";
}
return array($sets, array_keys($out), array_values($out));
}
?&gt;</pre>
<p>[/codesyntax]</p>
<p><strong>escape_arr.php</strong></p>
<p>[codesyntax lang="php"]</p>
<pre>&lt;?php
function escape_arr($arr)
{
$out=array();
$sets=array();
foreach ($arr as $key =&gt; $value)
{
$k="`$key`";
$v="'".mysql_real_escape_string($value)."'";
$out[$k] = $v;
$sets[]="$k = $v";
}
return array($sets, array_keys($out), array_values($out));
}
?&gt;</pre>
<p>[/codesyntax]</p>
<p>Although the above code is elegant, an associative array forces me to think of my queries as objects. After putting my head to work, I found that a blank INSERT could put my mind at ease.</p>
<blockquote><p>[codesyntax lang="php"]</p>
<pre>&lt;?php
$sql="INSERT INTO `tbl` (`id`) VALUES(NULL);";
$sql.="UPDATE `tbl` SET `f1`='v1',`f2`='v2' WHERE id = LAST_INSERT_ID();";
mysql_query($sql) or die(mysql_error());
?&gt;</pre>
<p>[/codesyntax]</p></blockquote>
<p>Conclusion: both solutions are equally valid. You may also hard-code your queries but such coding is bad practice. There are many drawbacks for using this approach.</p>
<ol>
<li>First of all, such strings can span several lines of code and make your PHP file hard to read and understand.</li>
<li>Secondly each one of your strings becomes a debugging red-flag.</li>
<li>And thirdly if you ever decided to add a new field to your table &#8211; say “birthdate” &#8211; then you will have to run through each of your hard-coded strings and add-in a field name and value for each of the strings respectively. Each string will then have an added chance for a syntax errors and make debugging harder. And since such coding practice lacks versatility, a simple task such as adding a “birthdate” field later in the future can become a nightmare.</li>
</ol>
<p>One could also use REPLACE. However I strongly advise against using this approach since it reserves the potential to wipe your database clean due to its DELETE + INSERT logic. A better solution would be INSERT … ON DUPLICATE KEY UPDATE. However this query doesn’t address my syntax difference emphasis.</p>
]]></content:encoded>
			<wfw:commentRss>http://vinothbabu.com/2010/05/08/update-and-insert-differences-in-syntax-is-an-inconvenience/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

