http://qs321.pair.com?node_id=318933

lwicks has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,
I am carrying on with my exploration of DBI and specifically DBD::Anydata and have a working script, but one of the sub routines comes up with the following error:

START of get_user_data<P> SELECT * FROM users WHERE id = ? [z] Use of uninitialized value at (ev +al 8) line 16. Use of uninitialized value at (eval 8) line 16. Use of + uninitialized value at (eval 8) line 16. Use of uninitialized value +at (eval 8) line 16. Use of uninitialized value at (eval 8) line 16. +Use of uninitialized value at (eval 8) line 16. Use of uninitialized +value at (eval 8) line 16. <P> result = z b b b b z NO Mon Jan 5 20:45:40 2004 Mon Jan 5 20:45:40 200 +4 0 50 1 0 0 0 Novice<P> User Data = z b b b b z NO Mon Jan 5 20:45:40 2004 Mon Jan 5 20:45:40 +2004 0 50 1 0 0 0 Novice<P> Judoka Limit = 1

The subroutine and script works fine (so far), but the error keeps showing up, which A) worries me and B) looks terrible on a user interface.

Any help would be appreciated. The sub routine is listed below

Cheers,
LANCE


sub get_user_data { # This subroutine collects the users data from t +he user database print p("START of get_user_data") if $DEBUG; my @internal_user_data = @_; # This line takes t +he user_data passed to us from the previous routine (@_) and allocate +s it to our internal user data array my $entered_id = $internal_user_data[0]; # just + so that it is clearer later we create a variable from the passed dat +a # Use DBI to connect to the users.csv datafile #---------------------------------------------- +- my $dbh = DBI->connect('dbi:AnyData(RaiseError= +>1):'); # tell DBI we want to use the Anydata module in ./MyLibs $dbh->func( 'users', 'CSV', 'data/users.csv', ' +ad_catalog'); # Connect to the users.csv data file # select from the datafile the id for the user +ID from the array paased from the previous sub routine my @params = ($entered_id); + # Theese are the parameteres we will use in the SQL command abo +ve my $sql = "SELECT * FROM users WHERE id = ?"; + # this is the SQL command we want to execute print "$sql\n[@params]\n" if $DEBUG; + # if we are in debug mode print the SQL statement my $sth = $dbh->prepare( $sql ); + # prepare the SQL command $sth->execute( @params ); + # excecute the SQL using our parameters my @result = $sth->fetchrow_array; # this line +takes the results of the select and puts it in the array called RESUL +TS $dbh->disconnect(); # we are done with the datb +ase for now, so disconnect from it (MAY NOT BE NECESSARY) print p("result = @result")if $DEBUG; + # Prints the result of our SQL command if we are in debug mode. return (@result); print p("END of get_user_data") if $DEBUG; }

Kia Kaha, Kia Toa, Kia Manawanui!
Be Strong, Be Brave, Be perservering!

janitored by ybiC: Retitle from less-than-descriptive "Why do I get this error?" for search-friendlyness, balanced <code> tags around error message for legibility

Replies are listed 'Best First'.
Re: DBD::Anydata, Use of uninitialized value at...
by bassplayer (Monsignor) on Jan 05, 2004 at 21:42 UTC
    It's a bit hard to tell without knowing which line is line 16 (if it's even here). I suggest looking at your code and determining which uninitialized value (on line 16) that the error is referring to. Then I would only execute the part in question once the variable was determined to have a value. Example:

    run_code_on_line_16( $variable ) if $variable;

    It seems from your output, that this line could be the culprit:

    print "$sql\n[@params]\n" if $DEBUG;

    Could @params be getting polluted somewhere?

    You might also check the values of @internal_user_data after you set them from @_. Perhaps there is something going wrong with the data being passed.

    I suppose you could always turn off warnings... :)

    bassplayer

Re: DBD::Anydata, Use of uninitialized value at...
by CountZero (Bishop) on Jan 05, 2004 at 22:35 UTC
    It's not wrong and probably did not cause the error, but why are you using my @params = ($entered_id) and putting a scalar in an array? It seems a bit "wasteful" to me. There is only one "?" which needs to be filled, so a scalar will do nicely.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      Cheers guys, the idea to check if the @params array was getting messed up was right on the money.

      Everything now works minus funny error, FAB!!

      Lance

      Kia Kaha, Kia Toa, Kia Manawanui!
      Be Strong, Be Brave, Be perservering!