in reply to Re: mySQL with Perl
in thread mySQL with Perl
Be careful: in a multi-user database, someone can insert the row between the select and insert statements. A better way to do this is to combine the existence test with the insert:
Note that $username appears twice, the first one to bind to the existence test. (BTW, I haven't tested this, and I'm assuming MySQL supports prepared exists clauses.)my $sql = qq{ if not exists (select 1 from members where username = ?) insert into members (username, password, email, name, url, date) values (?, ?, ?, ?, ?, ?) }; my $sth = $dbh->prepare($sql); $sth->execute($username, $username, $password, $email, $name, $url, $date)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: mySQL with Perl
by Coplan (Pilgrim) on Nov 11, 2002 at 21:52 UTC | |
by VSarkiss (Monsignor) on Nov 11, 2002 at 22:08 UTC |