get one column

From: Ing. Branislav Gerzo (2ge_at_2ge.us)
Date: 01/28/05


Date: Fri, 28 Jan 2005 09:55:53 +0100
To: dbi-users@perl.org

Hi all!

I have subroutine, that checks, if I already have in DB specified row:

sub image_exist {
  my ($id, $date) = @_; #id is _always_ number
  my $sth = $mydbh->prepare_cached("select ID from pic where id = ?");
  $sth->execute($id);
  my $rows = $sth->fetchall_arrayref();
  if ( @$rows[0] ) {
          print "$id exist\n";
  } else {
          print "$id not exist\n";
  }
}

I am pretty sure, this can be done better. Also I am curious about
this - when I change statement to:
my $sth = $mydbh->prepare_cached("select ID from pic where id = $id");
$sth->execute();

I get after some results:
Use of uninitialized value in concatenation (.) or string at D:\cygwin\home\Admi
nistrator\thumbs\insert_pic.pl line 81.
DBD::mysql::st execute failed: You have an error in your SQL syntax; check the m
anual that corresponds to your MySQL server version for the right syntax to use
near '' at line 1 at D:\cygwin\home\Administrator\thumbs\insert_pic.pl line 82.

Hm, strange...

I am using AS perl v5.8.6

-- 
Brano


Relevant Pages

  • Re: A very general question to perl experts
    ... Below I am writing the stub-form of my perl code. ... If I define all the subroutines in the sequence mentioned above then, I face lot of wierd problems in readDefectData function. ... You should use the Perl5 style of subroutine calls ... sub extractSPRL { ...
    (perl.beginners)
  • RE: returning hashes, and arrays
    ... :> subroutine named link(). ... :> array interchangeably here. ... : 'Learning Perl'; ... : sub ParseLineForHomeAndVisitors; ...
    (perl.beginners)
  • Re: Dynamically calling a subroutine
    ... CP> convenient to use the subroutine. ... something in perl. ... and if you can't have the name of the sub in advance, ... map input to loading the appropriate modules and calling the desired ...
    (perl.beginners)
  • RE: returning hashes, and arrays
    ... :> not to process anything that is not local to the sub. ... 'Beginning Perl' the author writes about doing it that way. ... Let's call a subroutine named link(). ... They do not declare a variable. ...
    (perl.beginners)
  • RE: returning hashes, and arrays
    ... > perl party. ... > What is declared on the subroutine declaration line ... > sub ParseLineForHomeAndVisitors{ ...
    (perl.beginners)