Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Populating an array from a mysql select

by Nik (Initiate)
on May 04, 2007 at 15:27 UTC ( [id://613591]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Populating an array from a mysql select
by Fletch (Bishop) on May 04, 2007 at 15:29 UTC

    Erm, yes because fetchrow_array fetches one row. You call it successive times until it returns an empty list (at which point the paranoid will check $select->errstr to see if anything went wrong).

    Update: Perhaps selectcol_arrayref and a suitable SELECT statement would be a better fit for what you're attempting? At any rate, read the DBI docs.

      Sorry for replying to you rather than to the OP, but he seems not to read at all what I write nor to care about it, while claiming he does: see the previous link for more info.

      So, from now on, I'm not replying him directly, but to the first reply to his posts: basically, this user

      • has the habit of posting the very same questions here and in clpmisc, often copying and pasting the very same text from one place to the other one and without indicating in any way doing so;
      • posts articles with so many spelling errors to be in any case a pita and in certain circumstances even hardly readable at all. All this, because in his own words, "he types too fast". (Of course in his world rereading them to be gentle to the people who will read what he writes, is not contemplated.)

      In this case:


      Nik, if by any chance you're reading this... please stop friggin' doing that!


      Update: I have to acknowledge that lately the person I'm talking about seems to have eventually understood.

Re: Populating an array from a mysql select
by rinceWind (Monsignor) on May 04, 2007 at 15:35 UTC

    The point is that fetchrow_array only fetches a single row. You want to use fetchall_arrayref instead.

    Warning: untested

    my @userlist = map {$_->[0]} @{$select->fetchall_arrayref};

    --
    wetware hacker
    (Qualified NLP Practitioner and Hypnotherapist)

      Thank you it does work :) but i didnt understood how as well i didnt understand why my
      @userlist = $select->fetchall_arrayref;
      returns a strign like HASH0xblabla...

        The reason is in what fetchall_arrayref returns. In general an SQL statement car return many columns, so you get array of rows, not an array of values. Each row in turn is an array ref or hash ref depending on how you call fetchrow_arrayref. In your case, you only have one column, and you can use fetchcol_arrayref to obtain that one column's data directly without havning to dereference anything for each row.

        Update: I misremembered the existence of a fetchcol_arrayref method. No such method exists; I was thinking of selectcol_arrayref which operates on a dbh, not a sth, and takes a statement as its first argument.

        A reply falls below the community's threshold of quality. You may see it by logging in.

        Allow me to explain.

        fetchall_arrayref returns an array ref, i.e. a scalar value which is a reference to an array (which will stringify as something like ARRAY(0x81503e8) if you try and print it out). You want to dereference this, and gain access to the array, hence the @{...}. This array has one element per row.

        Unfortunately, this is not quite exactly what you want, as it's an array of arrayrefs. Each of the referenced arrays only has one element, as your SQL is only returning one column, but would contain more than one value if your query was returning more columns (note: there is no such method as fetchcol_arrayref).

        The trick I have done is to fish out the element of each array, as the action for the map function. I could have written this as a for loop, but this kind of inline transformation lends itself to the builtins map and grep.

        For further reading, see perlreftut.

        --
        wetware hacker
        (Qualified NLP Practitioner and Hypnotherapist)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-25 04:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found