in reply to (Ovid) Re: uninitialized value in concatenation
in thread uninitialized value in concatenation

I'm sorry about not being more specific.
here is the code with the line numbers.
#Selecting parents name to identify as volunteer. 35 if ($parent eq "Father") 36 { 37 $sub_parent = "Mother"; 38 $sth = $dbh->prepare ("select Father from Roster where + User = \'$user\'"); 39 $sth->execute (); 40 41 $name = $sth->fetchrow_array; 42 } #end if Father 43 else 44 { 45 $sub_parent = "Father"; 46 $sth = $dbh->prepare ("select Mother from Roster where + User = \'$user\'"); 47 $sth->execute (); 48 49 $name = $sth->fetchrow_array; 50 } #end else Mother 51 52 if ($name eq ""){ 53 $sth = $dbh->prepare ("select $sub_parent from Roster +where User = \'$user\'"); 54 $sth->execute (); 55 56 $name = $sth->fetchrow_array; 57 58 } #end chk for not parent if statement. 59 60 print $query->h1({-align=>'center'},"THANK YOU FOR SIGNING UP +$name"); 61
I am certain it has something to do with returning an empty string from my sql as well but I don't know what that something is

Replies are listed 'Best First'.
Re(2): (Ovid): uninitialized value in concatenation
by dmmiller2k (Chaplain) on Jan 14, 2002 at 23:27 UTC

    When calling $sth->fetchrow_array in a scalar context, you're going to get back an array ref, possibly containing some other array refs (your result rows). So your test, if ($name eq "") {, should probably look more like, if ($name->[0][0] eq "") {, except that this completely disregards the case where no rows are returned.

    You might also factor out all of the common code and elminate unnecessary complexity:

    my $name = undef; # store subparent for each parent my %parent_hash = ( 'Father' => 'Mother', 'Mother' => 'Father' ); if (exists $parent_hash{$parent}) { my $sub_parent = $parent_hash{$parent}; for my $col ( $parent, $sub_parent ) { my $sql = "select $col from Roster where User=?"; my $rv = $dbh->selectcol_arrayref( $sql, undef, $user ); if ( @$rv ) { # any rows returned? $name = $rv->[0]; # take first one last; } } } # here, $name will be defined if one of above succeeded # etc. ...

    Also, why are you using prepared statements (e.g., prepare() and execute()) without placeholders ('?'). You can just call selectrow_arrayref($sql) (or in your case, selectcol_arrayref($sql) since the results will be a single column) to the same effect.

    Update: added placeholders, as per cfreak. What was I thinking?

    dmm

    If you GIVE a man a fish you feed him for a day
    But,
    TEACH him to fish and you feed him for a lifetime
      This code is not working because the sql is returning a '' value and the if statement is seeing it as being true. what can I change so it evaluates the '' as no value returned.
      # store subparent for each parent my %parent_hash = ( 'Father' => 'Mother', 'Mother' => 'Father' ); if (exists $parent_hash{$parent}) { my $sub_parent = $parent_hash{$parent}; for my $col ( $parent, $sub_parent ) { my $sql = "select $col from Roster where User=?"; my $rv = $dbh->selectcol_arrayref( $sql, undef, $user); if ( @$rv) { # any rows returned? $name = $rv->[0]; # take first one last; } } }

        "... the sql is returning a '' value and the if statement is seeing it as being true."

        This is not correct.

        As written, the if statement is not testing for truth or falseness, but whether or not any rows were returned.

        To accomplish what you seek, change the if statement to read:

        if ( @$rv && $rv->[0] ) { # any NON-BLANK rows returned?

        dmm

      In this
      my $name = undef; # store subparent for each parent my %parent_hash = ( 'Father' => 'Mother', 'Mother' => 'Father' ); if (exists $parent_hash{$parent}) { my $sub_parent = $parent_hash{$parent}; for my $col ( $parent, $sub_parent ) { my $sql = "select $col from Roster where User=?"; my $rv = $dbh->selectcol_arrayref( $sql, undef, $user ); if ( @$rv ) { # any rows returned? $name = $rv->[0]; # take first one last; } } } # here, $name will be defined if one of above succeeded
      what is the undef do inside the
      my $rv = $dbh->selectcol_arrayref( $sql, undef, $user );

        From the DBI docs:

        selectrow_array @row_ary = $dbh->selectrow_array($statement); @row_ary = $dbh->selectrow_array($statement, \%attr); @row_ary = $dbh->selectrow_array($statement, \%attr, @bind_values);

        where the third form is the one I was using, without specifying any attributes (i.e., undef).

        dmm

        From the DBI docs:

        selectrow_array @row_ary = $dbh->selectrow_array($statement); @row_ary = $dbh->selectrow_array($statement, \%attr); @row_ary = $dbh->selectrow_array($statement, \%attr, @bind_values);

        where the third form is the one I was using, without specifying any attributes (i.e., undef).

        dmm

        If you GIVE a man a fish you feed him for a day
        But,
        TEACH him to fish and you feed him for a lifetime
      When calling $sth->fetchrow_array in a scalar context, you're going to get back an array ref, possibly containing some other array refs (your result rows).

      It is not correct. $sth->fetchrow_array in a scalar context returns the value of the first field.

      --
      Ilya Martynov (http://martynov.org/)

      Please explain how I can eliminate some of the complexity.

        To start with, you could eliminate redundancies (as I've tried to demonstrate by refactoring your code), and encapsulate data dependencies in a data structure, to the degree possible (see my %parent_hash in my snippet), rather than if statements.