diyaz has asked for the wisdom of the Perl Monks concerning the following question:
sub insert_entry { my $table=$_[0]; #table my $col=$_[1]; my $entry=$_[2]; #value my $insert=sprintf(qq(INSERT INTO %s(%s) VALUES(%s) ON DUPLICATE K +EY UPDATE id=LAST_INSERT_ID(id)), $dbh->quote_identifier($table), $dbh->quote_identifier($col), $dbh->quote($entry)); my $sth=$dbh->prepare($insert); $sth->execute(); if ($sth->last_insert_id()){ #if new insertion return; } else { return $sth->last_insert_id(); #if duplicate return key } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: return primary key if duplicate entry exists?
by CountZero (Bishop) on Jan 24, 2016 at 09:36 UTC | |
by diyaz (Beadle) on Jan 24, 2016 at 15:44 UTC | |
by diyaz (Beadle) on Jan 24, 2016 at 15:50 UTC | |
by poj (Abbot) on Jan 24, 2016 at 16:07 UTC | |
by CountZero (Bishop) on Jan 24, 2016 at 19:12 UTC | |
| |
by diyaz (Beadle) on Jan 24, 2016 at 19:07 UTC | |
by CountZero (Bishop) on Jan 24, 2016 at 18:45 UTC | |
by diyaz (Beadle) on Jan 24, 2016 at 18:58 UTC | |
|