in reply to mySQL with Perl

Update:I cleaned up a redundant subroutine call in my code.

You need to get the information out with a custom sql statement and put it in some kind of data structure if it exists. There are a lot of ways to do this. a quick example.

#!/usr/bin/perl -w use strict; use DBI; #db connect my $driver = 'mysql'; my $database = 'usersdb'; my $dsn = "DBI:$driver:database=$database"; my $dbh = DBI->connect($dsn, undef, undef); my $user ="bob"; my $user_test = user_exists($user); if (defined $user_test){ print "user $user is already in our database"; }else{ #code to insert user } $dbh->disconnect; sub user_exists{ my $id_val = shift; my $statement = "select user_id from users where user_id = $id_val +"; my $ary_ref = $dbh->selectall_arrayref($statement); if (defined $ary_ref->[0]->[0]){ return $ary_ref->[0]->[0]; }else{ return undef; } }

Replies are listed 'Best First'.
Re: Re: mySQL with Perl
by mr.dunstan (Monk) on Mar 05, 2002 at 23:18 UTC
    select count(*) from user_table where username = $username

    is simpler if you ask me ... that way you only return a number instead of matching some kind of string or checking to see how many rows you've got.

    -mr.dunstan