MySQL Forums
Forum List  »  Perl

Perl DBI - 2nd SQL query affecting results of 1st?
Posted by: Rob Butler
Date: November 25, 2006 02:30PM

Hello,

I am current running FC6 with perl-5.8.8-5, perl-DBI-1.52-1.fc5, perl-DBD-MySQL-3.0004-1.FC5, mysql-server-5.0.22-1.FC5.1 and mysql-5.0.22-1.FC5.1 (all standard).

I have a perl CGI script which performs a select SQL query followed by an update query. However, the results of the select query are being distored by what seems to be the update query (the 2nd query) completing before the select query (1st query). I have included samples of the script below that reproduce this issue. Is this some kind of race condition and how do I work around it? I am using the same DBI connection and the same query handler for both queries.

Commenting out the second query resolves this issue. However the other strange problem I have encountered with this script is the second exit code seems to fail and both SQL queries are executed regardless of the condition matched surrounding this exit code.

Any help with this issue will be greatly appreciated, I am probably missing something really simple.

Thanks in advance,
Robin.

The code:

#!/usr/bin/perl -Tw
# Query.pl

use diagnostics;
use DBI;

my $hash = 'd09f1c4f759127f13db4e473e6648620';

# Start of HTML page.
print "Content-Type: text/html\n\n";
print <<HTML;
<html>
<head>
</head>
<ul>
<font face="Arial" size="4"><p>Account Creation:</p></font>
</ul>
HTML

# Verify user input syntax.
if ($hash !~ /^[a-f0-9]{1,128}$/) {
print '<p>Account authentication failed.</p>';
exit 0;
}

# Connect to database.
my $dbh = DBI->connect('DBI:mysql:fortress:localhost','username','password') or die "Could not connect to database: " . DBI->errstr;

# Retrieve an account based on the submitted md5 hex string if the account
# is awaiting activation.
$sth = $dbh->prepare('select username, password, email from members where md5=?') or die "Could not prepare statement: " . $dbh->errstr;
$sth->execute($hash) or die "Could not execute statement: " . $sth->errstr;

($username, $password, $email) = $sth->fetchrow_array();
if($sth->rows == 0) {
print '<ul>User account not found.</ul>';
print '</body></html>';
$sth->finish();
$dbh->disconnect();
exit 0;
} else {
$sth->finish();
}


# Remove the users md5 entry from the members table signifying the account is
# active.
$sth = $dbh->prepare('update members set md5="" where md5=?') or die "Could not prepare statement: " . $dbh->errstr;
$sth->execute($hash) or die "Could not execute statement: " . $sth->errstr;
$sth->finish;
$dbh->disconnect;


# Notify the user that the account is now activiated.
print <<HTML;
<ul><p>Your user account is now active!</p>
</body></html>
HTML

Options: ReplyQuote


Subject
Written By
Posted
Perl DBI - 2nd SQL query affecting results of 1st?
November 25, 2006 02:30PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.