Re: DBI: selectrow_array() return first column when called in list context

Mimi Cafe <[email protected]>
Newsgroups gmane.comp.lang.perl.modules.dbi.general
Message-ID <[email protected]>
This helped.

Thanks

Mimi



> On 23 Dec 2013, at 15:28, Bruce Johnson <[email protected]> wrote:
> 
> 
>> On Dec 23, 2013, at 2:30 AM, [email protected] wrote:
>> 
>> The SQL query is the same in both cases, and as can be seen, the query is expected to return two columns (user and perm), but selectrow_array() returns undef for the second column (perm in this case).
>> 
>> my ($x, $y)  = $dbh->selectrow_array(qq{SELECT user,perm FROM access
>> WHERE token=\'Tt9VpStL4xADSDJQtd4AkM
>> c6cVi66Mwmr9pMcCRgO4NVJ\'})|| die
>> "Could not query database:  $DBI::errstr\n";
>> 
>> In the above, $y is undef (or blank) although MySQL has values for both (user and perm) columns.
> 
> The problem, which Martin correctly noted, is that  selectrow_array returns a single thing, an array containing the returned data.
> 
> my ($x, $y)  = $dbh->selectrow_array(SQL);
> 
> is giving you a pointer to the returned array, $x, and an undef $y.
> 
> Because perl is lazy under some circumstances, when you say print $x it gives you $$x[0], whereas the value you expect in $y is actually in $$x[1]. (I've been bitten by this on occasion)
> 
> to get what you actually want you should do:
> 
> $arr = $dbh->selectrow_array(SQL);
> ($x, $y) = @$arr;
> 
> Now $x and $y contain what you expect them to.
> 
> (What Martin did in @retarr = @$arrref; )
> 
> 
> 
> -- 
> Bruce Johnson
> 
> "Wherever you go, there you are." B. Banzai, PhD
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.