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

Re: Saving array reference

by moot (Chaplain)
on Apr 30, 2005 at 19:59 UTC ( [id://452921]=note: print w/replies, xml ) Need Help??


in reply to Saving array reference

DBI::st appears to re-use internally an arrayref (I just got bitten by this today). You will have to store the record yourself:
my $first = undef; while (my $record = $sth->fetchrow_arrayref) { unless (defined $first) { @{$first} = @{$record} } # other stuff }
..although I'm unclear as to why you can't just stop retrieving records after the first, or why you can't just use a 'limit 1' clause in your SQL.

Also my DBI doesn't define a fetch method on statement handles, only fetchrow_* and fetchall_*, so you may need to re-jig my example.


Update: jZed++ for pointing out fetch is an alias for fetchrow_arrayref. Thanks, I've managed to miss that in every reading of perldoc DBI.

Replies are listed 'Best First'.
Re^2: Saving array reference
by jZed (Prior) on Apr 30, 2005 at 20:28 UTC
    fetch() is a synonym for fetchrow_arrayref().
Re^2: Saving array reference
by Errto (Vicar) on May 01, 2005 at 01:34 UTC
    Good suggestions. Just two minor points: first, in fairness to the DBI developers, the fact that fetchrow_arrayref reuses the same reference isn't just "internal", but is actually documented in the DBI docs:
    Note that the same array reference is returned for each fetch, so don’t store the reference and then use it after a later fetch.
    Second, the 'limit n' syntax isn't used across all databases, though for anyone on MySQL that's certainly a good suggestion. But Oracle, for example, doesn't have it.
Re^2: Saving array reference
by mhearse (Chaplain) on Apr 30, 2005 at 20:18 UTC
    Thanks, works great. You right though, I guess I could just do another query (strictly for the first match). Just wasn't thinking that way at the time. My thought was to save the first match for later use.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://452921]
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: (4)
As of 2024-04-25 07:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found