Christopher G Tantalo [EMAIL PROTECTED] wrote:
> In the code that follows, I want to return a 1 if there is an error, in 
> this case, my error would be if the select does not return a 
> sri_person_id.  I have tried this numerous ways, but have yet ot have it 
> run with without a warning message :
> "Use of uninitialized value in string eq at ./elead.pl line 222. "... 
> which is noted below.
> I have checked the docs and some old postings, which is how I got the 
> use of the length of the string to decide, but with warnings being used, 
> I still get the message.  ( and I do not want to turn warnings off)
> TIA,
> Chris
> 
> sub validate_sales_rep
> {
>        my $sr = shift @_;
>        # find sales_rep
>        my $sth = $dbh->prepare("SELECT sri_person_id from 
> lts.sales_rep_informa
> tion where sri_sales_rep = ?");
>        $sth->bind_param(1,$sr);
>        $sth->execute();
>        my $sr_id = $sth->fetchrow_array();

>From 'perldoc DBI':

       "fetchrow_array"
            @ary = $sth->fetchrow_array;
 
           An alternative to "fetchrow_arrayref". Fetches the next row of data
           and returns it as a list containing the field values.  Null fields
           are returned as "undef" values in the list.

Notice that you are accepting the results of $sth->fetchrow_array
into a *scalar* $sr_id - using fetchrow_array that should be
an array.  When you use a scalar in list context, the scalar
ends up containing the *number* of array elements.

But, you can also do it this way:

   my ($sr_id) = $sth->fetchrow_array();

that makes it list context, and now $sr_id should be what you
expect.  If you did something like

   my $sth = $dbh->prepare(q{
      SELECT sri_person_id,
             sri_person_name
      FROM   lts.sales_rep_information
      WHERE  sri_sales_rep = ?
   });

then your fetchrow_array would look like this:

   my ($sr_id, $sr_name) = $sth->fetchrow_array();

HTH.
-- 
Hardy Merrill
Red Hat, Inc.

Reply via email to