<?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; Programming</title>
	<atom:link href="http://vinothbabu.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://vinothbabu.com</link>
	<description>When Smart becomes Dumb!</description>
	<lastBuildDate>Thu, 31 Mar 2011 09:53:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</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>1</slash:comments>
		</item>
		<item>
		<title>Linklist and ArrayList</title>
		<link>http://vinothbabu.com/2010/03/23/linklist-and-arraylist/</link>
		<comments>http://vinothbabu.com/2010/03/23/linklist-and-arraylist/#comments</comments>
		<pubDate>Tue, 23 Mar 2010 13:07:17 +0000</pubDate>
		<dc:creator>Sachin</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://vinothbabu.com/?p=227</guid>
		<description><![CDATA[Today i was turning back towards Linklist and a question dropped in my mind is when to use Linklist and When to use ArrayList. They both are different in implementations. LinkedList implements it with a doubly-linked list. ArrayList implements it with a dynamically resizing array. If you take up Linklist you can walk the lists [...]]]></description>
			<content:encoded><![CDATA[<p>Today i was turning back towards Linklist and a question dropped in my mind is when to use Linklist and When to use ArrayList.<span id="more-227"></span></p>
<p>They both are different in implementations. LinkedList implements it with a doubly-linked list. ArrayList implements it with a dynamically resizing array. If you take up Linklist you can walk the lists forward or backwards, but grabbing an element in the middle takes time which depeneds upon your size of the list.</p>
<p>ArrayLists allows you random access, but adding and removing from anywhere smells your time which requires shifting all the latter elements. When you add more elements more than the capacity, a new array twice of the default size is allocated and the older array is copied to newer one.</p>
<p><strong>When should i use LinkedList?</strong></p>
<ol>
<li> When you need efficient removal in between elements or at the start.</li>
<li> When you don&#8217;t need random access to elements, but can live with iterating over them one by one</li>
</ol>
<p><strong><br />
When should i use ArrayList?</strong></p>
<ol>
<li> When you need random access to elements (&#8220;get the nth. element&#8221;)</li>
<li> When you don&#8217;t need to remove elements from between others. It&#8217;s possible but it&#8217;s slower since the internal backing-up array needs to be reallocated.</li>
<li> Adding elements is amortized constant time (meaning every once in a while, you pay some performance, but overall adding is instantly done)</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://vinothbabu.com/2010/03/23/linklist-and-arraylist/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

