perl, testing a database for a match

hi there.

in perl, I am struggling to find a simple method of connecting to a database, determining if the result of my query is "true" and then testing against the result. I dont even really want the data that i am 'SELECT'�ng. i effectively just want to check that a record exists with a UID column (primary key) that exactly matches the $uid that has been passed to my script.

I have managed to fudge a solution together that involves me having to

NOTE: the $uid is passed to script as an argument to the script

1) going to the db and doing "SELECT UID FROM table WHERE UID = $uid"

2) using fetchall_arrayref() to pull the result into an array (although should never be more than one return seeing as the data being requested is a primary key)

3) cycling through the array and sending each value to a fine in /tmp

seeing as this file will only ever contain something or nothing, i do a test later on the script to see if the file has content, if it does, i know that my query was "true" so to speak and I can be sure this a record for the $uid passed to my script in the database

I know that this seems a ridiculously over the top way to do this, but i am relatively new to perl and it was the only way i could get it to work.

# this value will in reality be passed to this script via a getopts cli argument
my $uid = '1234'     #as an example

my $dbh = DBI->connect("DBI:mysql:DB1","user","pass",) or die $DBI::errstr;
my $sth = $dbh->prepare("SELECT UID FROM table WHERE UID = $uid") or die $DBI::errstr;
       
 $sth->execute or die $DBI::errstr;
        
my $finalref = $sth->fetchall_arrayref();


        
foreach (@$finalref) {
      system("echo @$_ >> /tmp/results");
}

I just wondered if any of the fine gurus on this site could advise me a way of doing a simple 1/2 line version of the above without all the faffing about that ive had to do :). bear in mind, all i need to do is test that a record exists, i dont even need to pull any data from the DB or send any to it at this stage

any help on this would be greatly appreciated

After selecting the data you could build your logic using:

$sth->rows

You don't need to fetch any data.

P.S. You don't even need real data in the select list:

select null from table where ... 

and then:

$sth->rows and continue here ... or just stop.

No rows will be evaluated as false in boolean context.

my $uid = '1234'     #as an example

my $db = DBI->connect("DBI:mysql:DB1","user","pass",) or die $DBI::errstr;
my $sql= qq~SELECT UID FROM table WHERE UID = $uid~; 

my $rv = $db->selectcol_arrayref( $sql )->[0];

$rv ? print "We Have UID $rv\n" : print "No UID $rv found\n";