Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: selectrow_array() confusion

by Cody Pendant (Prior)
on Nov 25, 2008 at 21:35 UTC ( [id://725954]=note: print w/replies, xml ) Need Help??


in reply to selectrow_array() confusion

To answer the questions:
  • definitely connecting to the same db in both cases
  • that's my actual code in all salient points
  • it works with the hashref
  • it gets the right thing in $min
I'm on DBD version 1.48 but I can't figure out what version of the mysql driver it is. MySQL is 5.1.25-rc, and it's on OS X.

Unfortunately I need to be away from this machine for a while. Thanks for the help so far.



Nobody says perl looks like line-noise any more
kids today don't know what line-noise IS ...

Replies are listed 'Best First'.
Re^2: selectrow_array() confusion
by Anonymous Monk on Nov 25, 2008 at 23:51 UTC

    To get DBD::mysql version do:

    perl -MDBD::mysql -le 'print $DBD::mysql::VERSION;'

    To get DBI versions do:

    perl -MDBI -le 'print $DBI::VERSION;'

    selectrow_array is built into each DBD so if you upgrade DBI you need to rebuild DBD::mysql. Recent versions of DBI (1.605 - http://search.cpan.org/~timb/DBI-1.607/Changes) contain some fixes for selectrow_array but I don't think ones that affect what you are doing. However, I'm guessing you have upgraded DBI and not rebuilt DBD::mysql as at present I cannot think of any other reason for this.

      I'm guessing you have upgraded DBI and not rebuilt DBD::mysql
      That is indeed possible. I'll check as soon as I can.


      Nobody says perl looks like line-noise any more
      kids today don't know what line-noise IS ...
      The $DBD::mysql version is 4.007.


      Nobody says perl looks like line-noise any more
      kids today don't know what line-noise IS ...
Re^2: selectrow_array() confusion
by graff (Chancellor) on Nov 26, 2008 at 02:18 UTC
    You said "it works with the hashref", which I gather means you get the right answer when you do:
    my $href = $d->selectall_hashref( ... );
    If that's true, then maybe you haven't shown all the relevant code. In particular, after you call $d->selectrow_array(...) how many other things happen before you notice that $max is undef? And do any of those things involve $max (in ways that might cause it to become undef)?

    (UPDATE: I should point out that I use DBI and Mysql on OSX, and the "selectrow_array" call works for me when I do the equivalent type of query and list assignment on one of my databases.)

      I really have shown you the right code, honestly.

      This is a minimum test case

      #!/sw/bin/perl use strict; use warnings; use DBI; my $d = DBI->connect( 'DBI:mysql:imagedb:localhost', 'root', '******', { RaiseError => 1 } ) || die("couldn't connect to database"); my ( $min, $max ) = $d->selectrow_array( "SELECT min(id),max(id) from images where gallery_path like '/px6% +'") || die $d->errstr; print "min: $min; max: $max;\n";
      And when I run it, I get:
      Use of uninitialized value in concatenation (.) or string at ab.cgi li +ne 13. min: 61893; max: ;


      Nobody says perl looks like line-noise any more
      kids today don't know what line-noise IS ...
        "SELECT min(id),max(id) from images where gallery_path like '/px6%' +") || die $d->errstr;

        should be

        "SELECT min(id),max(id) from images where gallery_path like '/px6%' +") or die $d->errstr;

        You are forcing the selectrow_array call to be in scalar context by use of the "c-style logical or" || (see the precedence chart in perlop). You need to use or. Quoth the docs:

        If called in a scalar context for a statement handle that has more than one column, it is undefined whether the driver will return the value of the first column or the last. So don't do that. Also, in a scalar context, an "undef" is returned if there are no more rows or if an error occurred. That "undef" can't be distinguished from an "undef" returned because the first field value was NULL. For these reasons you should exercise some caution if you use "selectrow_array" in a scalar context, or just don't do that.

        The meme for spewing errors with a postfix or die uses "or" for a reason. It binds lower than all other operations, so it will be executed only if the entire statement itself fails (well, almost).

        You have this error in the line above as well. I would also change your connect call to use the or meme instead of the || one.

        As a learning device for others: this is a reason to include a complete working example of the failing code. Many (if not most) of the responses above were focusing on DBI/DBD, when in reality the problem was internal to perl operator misuse. That was not discovered until a complete code example was posted that exhibited the behavior.

        and just so this gets picked up as an example when searching:

        • or vs ||

        --MidLifeXis

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://725954]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-03-29 11:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found