Friday, August 7, 2009

REPLACE Statement on MySQL

Many times we need to update a record but we might not know whether such a record exists or not. In traditional programming we usually check whether the record exists or not, then perfrom the appropriate query according to the result of the previous query. A very good example of such situatuion is where you need to update the ip of an user against an user id, but the id may or may not be present in the visits table, user_id being the primary key for the table visits.

An usual solution to the problem is something like this.

Code: Perl
my $user_id = 1000;
my $ip = $ENV{'REMOTE_ADDR'};
my $stmt = $db->prepare('SELECT * FROM visits WHERE user_id=?');
$stmt->execute($user_id);

if($stmt->rows) ## a entry for the page id exists, update the record
{
my $update_stmt = $db->prepare('UPDATE visits SET ip = ? WHERE user_id=?');
$update_stmt->execute($ip,$user_id);
}
else ## no such id, insert a new entry with count 1
{
my $insert_stmt = $db->prepare('INSERT INTO visits VALUES(?,?)');
$insert_stmt->execute($user_id,$ip);
}

Code: PHP
$user_id = 1000;
$ip = $_SERVER['REMOTE_ADDR'];
$res = mysql_query("SELECT * FROM visits WHERE user_id=$user_id");

if(mysql_num_rows($res)) // a entry for the page id exists, update the record
{
$result = mysql_query("UPDATE visits SET ip = '$ip' WHERE user_id=$user_id");
}
else // no such id, insert a new entry with count 1
{
$result = mysql_query("INSERT INTO visits VALUES('$user_id','$ip')");
}

Enter: REPLACE

The REPLACE statement very similarly or exactly like INSERT statment, except for the fact that in case a record with the same primary key exists in the one that's going to be inserted then the old row is deleted before the new one is inserted. You need to have an unique index or a primary key to use REPLACE statment.

REPLACE is not part of the SQL standard, it's an useful extension by MySQL, I guess there are many other RDBMSs which have come up with similar extensions.

So, now that we have REPLACE, we can rewrite above code snippets in the following manner:

Code: Perl
my $user_id = 1000;
my $ip = $ENV{'REMOTE_ADDR'};
my $stmt = $db->prepare('REPLACE INTO visits VALUES(?,?)');
$stmt->execute($user_id,$ip);

Code: PHP
$user_id = 1000;
$ip = $_SERVER['REMOTE_ADDR'];
$res = mysql_query("REPLACE INTO visits VALUES('$user_id','$ip')");

That's it, isn't that easier guys!!

Read more about REPLACE statement here, http://dev.mysql.com/doc/refman/5.0/en/replace.html

Alternative Approach

A forgotten or unknown feature among the masses is the ON DUPLICATE KEY UPDATE, it causes an UPDATE when a duplicate key error is encountered during an insert. The above code snippet again be re-written like this,

Code: Perl
my $user_id = 1000;
my $ip = $ENV{'REMOTE_ADDR'};
my $stmt = $db->prepare('INSERT INTO visits VALUES (?,?) ON DUPLICATE KEY UPDATE ip=?');
$stmt>execute($user_id,$ip,$ip);

Code: PHP
$user_id = 1000;
$ip = $_SERVER['REMOTE_ADDR'];
$res = mysql_query("INSERT INTO visits VALUES ('$user_id','$ip') ON DUPLICATE KEY UPDATE ip='$ip'");

You can read more about this feature here, http://dev.mysql.com/doc/refman/5.0/...duplicate.html

Monday, May 25, 2009

Google Chrome 2.0 now available for download


If you're like me who's using Chrome for quite sometime now, then you'll be excited to know that they've released the Google Chrome 2.0, which boasts 20% increase in browser performance than Chrome 1. With it's simple design hides a technology sophisticatedly integrated to provide faster, safer and easier web browsing experience.

Download it now at google.com\chrome and see for yourself!

Wednesday, May 20, 2009

Tips on improving your site's page visit | page ranking | traffic




Hi! I have gathered some websites that explains you a lot of creative ways on how to
improve your web site's page ranking and page visits. This is very crucial for online marketers and web designers alike if they wish to see their websites being found on search engines like google, yahoo and etcetera. Please find time to read these tips and start your way now to improving your website's statistics.






reputation management tips: http://www.moregoogletraffic.com/



I hope this helps you!

Monday, May 18, 2009

13 Amazingly Rich Resources for Web Development - CSS | Web Layout | Javascripts | Colors





Free CSS templates
http://www.opendesigns.org/

Automatic generator of backend codes

Provides over 500 layouts to use in planning and designing websites
http://www.layoutcookbook.com/

Formats and Optimizes your CSS
www.cleancss.com/

Dreamveawer color codes
www.buttonator.com/

Fast and concise JavaScript Library
http://jquery.com/

Free CSS templates downloadable in .zip format
http://www.free-css-templates.com/

Rich variety of useful photoshop tutorials on creating web layouts
http://sixrevisions.com/web_design/40-useful-photoshop-web-layout-tutorials/

Open source service that creates screenshots of your web design in different browsers
www.browsershots.org/

Very good list of patterns for your web layouts
http://www.welie.com/patterns/

Another great list of CSS Resource

Monday, April 27, 2009

Your Photography Costs What!?

One of the essentials in organizing a wedding is finding a photographer, I recently had this urge to discover how come photo service cost so much... I mean from my point of view it's just a simple matter of pointing the cam and clicking, right? So what's the deal about it? Read Why Wedding Photos Can Be Expensive

Tuesday, April 21, 2009

MY MANHOLE EXPERIENCE




Two weeks ago I found myself dumped in a manhole. It started while I was closely attending someone on the rooftop in the office where I work, looking constantly upward as I was on the ground, I didn’t noticed that I was stepping on a square cement-made cover for an opening, a hole; it seems that the weight of my body was too much for the cover to bear and at the slight moment the cover fell off with me still on top of it.

As I was rushing downwards I had the presence of mind to use both hands to push against the ground so that it would somehow break the momentum of my fall, but it was kind of a second late because I found out that both my feet were already soaked on dirty, smelly, murky water. I immediately pulled myself out of that pit and head straight to the closest source of running water around and washed my sneakers and some portions of my pants. I got some minor bruises on my hands and legs and so I asked my colleague if I could go home for a while to change my clothes and be back after lunch.

It’s definitely the most embarrassing moment of my life. Being in a situation where I least expected the most would happen to me made me feel so uncomfortable, I felt all of a sudden all eyes were on me and I didn’t know what to do. “What’s this?” I asked myself, the feeling of your feet being soaked deep on the dirt of people was so humiliating, I will not forget the horrifying smell my socks bore that time.

But somehow that experience made me remember one important lesson in my life… I was reminded of a person who shared the same experience as I did, only it was a hundred folds worst than what I experienced. My embarrassment when I fell on that pit was no match for how degrading people have treated Him, the humiliation I got was not even close to what he had when his robe was being torn apart by the roman soldiers and even before he was being nailed on a log he suffered wounds, cuts and lacerations so deep he could hardly walk. I was reminded how God loved me what the Son had to go through because of that love.

Is 54:10 God assured his people that even if the mountains be shaken and the hills being removed, never will His unfailing love depart from us.

Lam 3: 32

Ps 33:18

God is love and since He lives eternally, so is His love will endure forever.

Thursday, April 16, 2009

Nalusuan Island Revisit


Summer's definitely here again and what better way to drench that hot wave out of our skin than to be on an island where beauty magnifies everywhere; the water surrounding the island will surely captivate your heart and would find diving in it irresistable. The marine sanctuary will marvel you with breath-taking views of sea creatures rushing from all areas.


Be sure to taste this adventure of a lifetime! Visit Nalusuan Island and make your summer vacation experience a thing that will last a lifetime!