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

"[email protected]" <[email protected]>
Newsgroups gmane.comp.lang.perl.modules.dbi.general
Message-ID <CAOqrB6ZdLEhAk6oV=r-QzoAotSd6-WWSbRm+7mpiqWWueX2fnA@mail.gmail.com>
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.



On 21 December 2013 08:23, Martin J. Evans <[email protected]> wrote:

> On 20/12/2013 14:13, [email protected] wrote:
>
>> Hi
>>
>> I need to understand why selectrow_array() return only one column (first
>> column) when called in list context as in the code below. On the
>> otherhand selectrow_arrayref behaves correctly.
>>
>> code
>> =====
>>
>
> You've missed out how you actually called selectrow_array and that is the
> important bit.
>
> I suspect you are not calling it in list context.
>
>
>  if ($x){
>>          print "When using selectrow_array(), DBI returned:";
>>          print "\$x: $x \$y: $y";
>>
>>          print "\n\n";
>> }else{
>>          print "DB returned no data\n\n";
>>
>> }
>>
>> selectrow_arrayref behaves correctly.
>>
>> my ($arrref) = $dbh->selectrow_arrayref(qq{
>>
>
> You don't need brackets around $arrref here BTW. selectrow_arrayref only
> returns one value.
>
>  SELECT user,perm FROM access
>> WHERE token=\'Tt9VpStL4xADSDJQtd4AkMc6cVi66Mwmr9pMcCRgO4NVJ\'})|| die
>> "Could not query database:  $DBI::errstr\n";
>>
>> my @retarr = @$arrref;
>>
>> if (@retarr){
>>          print "When using selectrow_arrayref, DBI returned:";
>>          print "\@retarr contains: @retarr ";
>>
>>          print "\n\n";
>> }else{
>>          print "DB returned no data\n\n";
>>
>> }
>>
>>
>> Output
>> =========
>> perl misc_test.pl <http://misc_test.pl>
>>
>> Use of uninitialized value $y in concatenation (.) or string at
>> misc_test.pl <http://misc_test.pl> line 69.
>>
>> When using selectrow_array(), DBI returned:$x: markb $y:
>>
>> When using selectrow_arrayref, DBI returned:@retarr contains: markb ro
>>
>>
>> Installed versions
>> ============
>> perl -MDBD::mysql -le 'print $DBD::mysql::VERSION;'
>> 4.013
>>   perl -MDBI -le 'print $DBI::VERSION;'
>> 1.609
>>
>
> Martin
> --
> Martin J. Evans
> Wetherby, UK
>
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.