perl fetchrow_array возвращает только последний столбец вместо всех столбцов
Я работаю над сценарием под названием "hlstats.pl", который является сервером статистики из игры Half-Life от http://www.hlstats-community.org/
Я пытаюсь запустить это на Strawberry Perl на Windows с MySQL.
Я изолировал проблему для fetchrow_array, возвращая только тот последний столбец в запросе.
Вот мой модифицированный фрагмент кода из hlstats.pl
# load the options from DB
my $result = &doQuery("SELECT keyname, value FROM `${db_prefix}_Options`");
my @row;
while (@row = $result->fetchrow_array()) {
foreach $field (@row) {
if (defined($field)) {
print "$field\n";
} else {
print "<NULL>\n";
}
}
}
##my ($keyname, $value, %oHash);
##while( ($keyname, $value) = $result->fetchrow_array ) {
## # $oHash{$keyname} = $value;
## print $keyname;
## print "\n";
##}
$result->finish();
Он выведет все значения в столбце "значение", если он последний, и я могу поменять его, и он выведет все значения в столбце "имя ключа", если я переместлю их в запросе. Мне не понятно, почему мы не получаем обратно все столбцы.
Это Perl, v5.10.0 построен для MSWin32-x86-многопоточность
MySQL версия 5.7.10
вывод perldoc perllocal
Thu Jan 29 21:17:27 2009: "Module" ExtUtils::MakeMaker
* "installed into: C:\strawberry\perl\lib"
* "LINKTYPE: dynamic"
* "VERSION: 6.48"
* "EXE_FILES: bin/instmodsh"
Thu Jan 29 21:17:32 2009: "Module" File::Path
* "installed into: C:\strawberry\perl\lib"
* "LINKTYPE: dynamic"
* "VERSION: 2.07"
* "EXE_FILES: "
Thu Jan 29 21:17:38 2009: "Module" ExtUtils::Command
* "installed into: C:\strawberry\perl\lib"
* "LINKTYPE: dynamic"
* "VERSION: 1.16"
* "EXE_FILES: "
Thu Jan 29 21:17:47 2009: "Module" Win32API::File
* "installed into: C:\strawberry\perl\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.1101"
* "EXE_FILES: "
Thu Jan 29 21:18:05 2009: "Module" ExtUtils::Install
* "installed into: C:\strawberry\perl\lib"
* "LINKTYPE: dynamic"
* "VERSION: 1.52"
* "EXE_FILES: "
Thu Jan 29 21:18:10 2009: "Module" ExtUtils::Manifest
* "installed into: C:\strawberry\perl\lib"
* "LINKTYPE: dynamic"
* "VERSION: 1.56"
* "EXE_FILES: "
Thu Jan 29 21:18:34 2009: "Module" Test::Harness
* "installed into: C:\strawberry\perl\lib"
* "LINKTYPE: dynamic"
* "VERSION: 3.14"
* "EXE_FILES: bin/prove"
Thu Jan 29 21:18:43 2009: "Module" Test::Simple
* "installed into: C:\strawberry\perl\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.86"
* "EXE_FILES: "
Thu Jan 29 21:18:48 2009: "Module" ExtUtils::CBuilder
* "installed into: C:\strawberry\perl\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.24"
* "EXE_FILES: "
Thu Jan 29 21:18:54 2009: "Module" ExtUtils::ParseXS
* "installed into: C:\strawberry\perl\lib"
* "LINKTYPE: dynamic"
* "VERSION: 2.19"
* "EXE_FILES: "
Thu Jan 29 21:19:05 2009: "Module" version
* "installed into: C:\strawberry\perl\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.76"
* "EXE_FILES: "
Thu Jan 29 21:19:11 2009: "Module" IO::Compress::Base
* "installed into: C:\strawberry\perl\lib"
* "LINKTYPE: dynamic"
* "VERSION: 2.015"
* "EXE_FILES: "
Thu Jan 29 21:19:23 2009: "Module" Compress::Raw::Zlib
* "installed into: C:\strawberry\perl\lib"
* "LINKTYPE: dynamic"
* "VERSION: 2.015"
* "EXE_FILES: "
Thu Jan 29 21:19:33 2009: "Module" Compress::Raw::Bzip2
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 2.015"
* "EXE_FILES: "
Thu Jan 29 21:20:34 2009: "Module" IO::Compress::Zlib
* "installed into: C:\strawberry\perl\lib"
* "LINKTYPE: dynamic"
* "VERSION: 2.015"
* "EXE_FILES: "
Thu Jan 29 21:20:47 2009: "Module" IO::Compress::Bzip2
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 2.015"
* "EXE_FILES: "
Thu Jan 29 21:20:55 2009: "Module" Compress::Zlib
* "installed into: C:\strawberry\perl\lib"
* "LINKTYPE: dynamic"
* "VERSION: 2.015"
* "EXE_FILES: "
Thu Jan 29 21:21:18 2009: "Module" Compress::Bzip2
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 2.09"
* "EXE_FILES: "
Thu Jan 29 21:21:24 2009: "Module" IO::Zlib
* "installed into: C:\strawberry\perl\lib"
* "LINKTYPE: dynamic"
* "VERSION: 1.09"
* "EXE_FILES: "
Thu Jan 29 21:21:31 2009: "Module" Cwd
* "installed into: C:\strawberry\perl\lib"
* "LINKTYPE: dynamic"
* "VERSION: 3.29"
* "EXE_FILES: "
Thu Jan 29 21:21:53 2009: "Module" File::Temp
* "installed into: C:\strawberry\perl\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.21"
* "EXE_FILES: "
Thu Jan 29 21:22:03 2009: "Module" Win32::WinError
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.03"
* "EXE_FILES: "
Thu Jan 29 21:22:14 2009: "Module" Win32API::Registry
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.30"
* "EXE_FILES: "
Thu Jan 29 21:22:19 2009: "Module" Win32::TieRegistry
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.25"
* "EXE_FILES: "
Thu Jan 29 21:22:24 2009: "Module" File::HomeDir
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.82"
* "EXE_FILES: "
Thu Jan 29 21:22:29 2009: "Module" File::Which
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.05"
* "EXE_FILES: bin/pwhich"
Thu Jan 29 21:22:42 2009: "Module" Archive::Zip
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 1.26"
* "EXE_FILES: bin/crc32"
Thu Jan 29 21:22:46 2009: "Module" Package::Constants
* "installed into: C:\strawberry\perl\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.02"
* "EXE_FILES: "
Thu Jan 29 21:22:51 2009: "Module" IO::String
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 1.08"
* "EXE_FILES: "
Thu Jan 29 21:22:59 2009: "Module" Archive::Tar
* "installed into: C:\strawberry\perl\lib"
* "LINKTYPE: dynamic"
* "VERSION: 1.44"
* "EXE_FILES: bin/ptar bin/ptardiff"
Thu Jan 29 21:23:04 2009: "Module" Parse::CPAN::Meta
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.04"
* "EXE_FILES: "
Thu Jan 29 21:23:16 2009: "Module" YAML
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.68"
* "EXE_FILES: "
Thu Jan 29 21:23:23 2009: "Module" Digest::MD5
* "installed into: C:\strawberry\perl\lib"
* "LINKTYPE: dynamic"
* "VERSION: 2.38"
* "EXE_FILES: "
Thu Jan 29 21:23:29 2009: "Module" Digest::SHA1
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 2.11"
* "EXE_FILES: "
Thu Jan 29 21:23:38 2009: "Module" Digest::SHA
* "installed into: C:\strawberry\perl\lib"
* "LINKTYPE: dynamic"
* "VERSION: 5.47"
* "EXE_FILES: shasum"
Thu Jan 29 21:25:58 2009: "Module" CPAN
* "installed into: C:\strawberry\perl\lib"
* "LINKTYPE: dynamic"
* "VERSION: 1.9301"
* "EXE_FILES: scripts/cpan"
Thu Jan 29 21:26:05 2009: "Module" Term::ReadKey
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 2.30"
* "EXE_FILES: "
Thu Jan 29 21:26:09 2009: "Module" Term::ReadLine
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 1.0302"
* "EXE_FILES: "
Thu Jan 29 21:26:13 2009: "Module" Text::Glob
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.08"
* "EXE_FILES: "
Thu Jan 29 21:26:24 2009: "Module" URI
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 1.37"
* "EXE_FILES: "
Thu Jan 29 21:26:28 2009: "Module" HTML::Tagset
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 3.20"
* "EXE_FILES: "
Thu Jan 29 21:26:41 2009: "Module" HTML::Parser
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 3.59"
* "EXE_FILES: "
Thu Jan 29 21:27:18 2009: "Module" LWP
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 5.823"
* "EXE_FILES: bin/lwp-request bin/lwp-mirror bin/lwp-rget
bin/lwp-download"
Thu Jan 29 21:27:31 2009: "Module" LWP::Online
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 1.07"
* "EXE_FILES: "
Thu Jan 29 21:27:56 2009: "Module" Win32::File
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.06"
* "EXE_FILES: "
Thu Jan 29 21:28:17 2009: "Module" File::Remove
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 1.42"
* "EXE_FILES: "
Thu Jan 29 21:28:21 2009: "Module" Win32::File::Object
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.01"
* "EXE_FILES: "
Thu Jan 29 21:28:45 2009: "Module" Win32::API
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.58"
* "EXE_FILES: "
Thu Jan 29 21:29:05 2009: "Module" Params::Util
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.35"
* "EXE_FILES: "
Thu Jan 29 21:29:14 2009: "Module" IPC::Run3
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.042"
* "EXE_FILES: "
Thu Jan 29 21:29:19 2009: "Module" Test::Script
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 1.03"
* "EXE_FILES: "
Thu Jan 29 21:29:24 2009: "Module" Win32::Env::Path
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.01"
* "EXE_FILES: script/win32envpath"
Thu Jan 29 21:29:46 2009: "Module" Parse::Binary
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.11"
* "EXE_FILES: "
Thu Jan 29 21:29:51 2009: "Module" Win32::Exe
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.11"
* "EXE_FILES: script/exe_update.pl"
Thu Jan 29 21:30:32 2009: "Module" Math::BigInt
* "installed into: C:\strawberry\perl\lib"
* "LINKTYPE: dynamic"
* "VERSION: 1.89"
* "EXE_FILES: "
Thu Jan 29 21:30:52 2009: "Module" Math::BigInt::FastCalc
* "installed into: C:\strawberry\perl\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.19"
* "EXE_FILES: "
Thu Jan 29 21:31:14 2009: "Module" Math::BigRat
* "installed into: C:\strawberry\perl\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.22"
* "EXE_FILES: "
Thu Jan 29 21:31:37 2009: "Module" Math::BigInt::GMP
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 1.24"
* "EXE_FILES: "
Thu Jan 29 21:31:49 2009: "Module" XML::Parser
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 2.36"
* "EXE_FILES: "
Thu Jan 29 21:32:18 2009: "Module" XML::NamespaceSupport
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 1.09"
* "EXE_FILES: "
Thu Jan 29 21:32:28 2009: "Module" XML::SAX
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.96"
* "EXE_FILES: "
Thu Jan 29 21:32:41 2009: "Module" XML::LibXML::Common
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.13"
* "EXE_FILES: "
Thu Jan 29 21:33:01 2009: "Module" XML::LibXML
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 1.69"
* "EXE_FILES: "
Thu Jan 29 21:33:30 2009: "Module" PAR::Dist
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.43"
* "EXE_FILES: "
Thu Jan 29 21:33:37 2009: "Module" PAR::Dist::FromPPD
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.03"
* "EXE_FILES: bin/ppd2par"
Thu Jan 29 21:33:42 2009: "Module" PAR::Dist::InstallPPD
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.02"
* "EXE_FILES: bin/parinstallppd"
Thu Jan 29 21:34:18 2009: "Module" IO::Stringy
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 2.110"
* "EXE_FILES: "
Thu Jan 29 21:34:41 2009: "Module" Tree::DAG_Node
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 1.06"
* "EXE_FILES: "
Thu Jan 29 21:35:03 2009: "Module" Test::Warn
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.11"
* "EXE_FILES: "
Thu Jan 29 21:35:21 2009: "Module" Test::Tester
* "installed into: C:\strawberry\perl\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.107"
* "EXE_FILES: "
Thu Jan 29 21:35:27 2009: "Module" Test::NoWarnings
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.084"
* "EXE_FILES: "
Thu Jan 29 21:35:34 2009: "Module" Test::Deep
* "installed into: C:\strawberry\perl\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.103"
* "EXE_FILES: "
Thu Jan 29 21:35:39 2009: "Module" DBM::Deep
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 1.0013"
* "EXE_FILES: "
Thu Jan 29 21:35:59 2009: "Module" YAML::Tiny
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 1.36"
* "EXE_FILES: "
Thu Jan 29 21:36:23 2009: "Module" AutoLoader
* "installed into: C:\strawberry\perl\lib"
* "LINKTYPE: dynamic"
* "VERSION: 5.68"
* "EXE_FILES: "
Thu Jan 29 21:36:29 2009: "Module" PAR
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.984"
* "EXE_FILES: "
Thu Jan 29 21:36:34 2009: "Module" PAR::Repository::Query
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.12"
* "EXE_FILES: "
Thu Jan 29 21:36:39 2009: "Module" PAR::Repository::Client
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.21"
* "EXE_FILES: "
Thu Jan 29 21:36:49 2009: "Module" PPM
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.01_01"
* "EXE_FILES: bin/ppm.pl"
Thu Jan 29 21:37:09 2009: "Module" Number::Compare
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.01"
* "EXE_FILES: "
Thu Jan 29 21:37:19 2009: "Module" pler
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.34"
* "EXE_FILES: script/pler"
Thu Jan 29 21:37:44 2009: "Module" Data::Compare
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 1.21"
* "EXE_FILES: "
Thu Jan 29 21:37:50 2009: "Module" CPAN::Checksums
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 2.02"
* "EXE_FILES: "
Thu Jan 29 21:37:56 2009: "Module" File::chmod
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.32"
* "EXE_FILES: "
Thu Jan 29 21:38:01 2009: "Module" CPAN::Inject
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.11"
* "EXE_FILES: script/cpaninject"
Thu Jan 29 21:38:15 2009: "Module" pip
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.13"
* "EXE_FILES: script/pip"
Thu Jan 29 21:39:33 2009: "Module" DBI
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 1.607"
* "EXE_FILES: dbiproxy dbiprof dbilogstrip"
Thu Jan 29 21:40:00 2009: "Module" DBD::SQLite
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 1.14"
* "EXE_FILES: "
Thu Jan 29 21:40:20 2009: "Module" CPAN::DistnameInfo
* "installed into: C:\strawberry\perl\site\lib"
* "LINKTYPE: dynamic"
* "VERSION: 0.07"
* "EXE_FILES: "
use strict;
use warnings;
use DBI;
print map "$_\n",
DBI->available_drivers;
Выход:
- DBM
- ExampleP
- файл
- Gofer
- мультиплекс
- полномочие
- SQLite
- губка
- MySQL
1 ответ
Весьма вероятно, что ваш реальный код имеет это
while ( $row = $result->fetchrow_array() ) {
foreach $field ( $row ) {
if ( defined($field) ) {
print "$field\n";
}
else {
print "<NULL>\n";
}
}
}
Документация дляfetchrow_array
говорит это
При вызове в скалярном контексте для дескриптора оператора, который имеет более одного столбца, не определено, будет ли драйвер возвращать значение первого или последнего столбца. Так что не делай этого.
Я бы написал это
while ( my @row = $result->fetchrow_array ) {
print "$_\n" for map { $_ // '<NULL>' } @row;
}
или вы можете использовать fetchrow_arrayref
как это
while ( my $row = $result->fetchrow_arrayref ) {
print "$_\n" for map { $_ // '<NULL>' } @$row;
}
Может быть очень трудно обнаружить ошибку, когда мы не можем видеть то, что вы видите. Пожалуйста, будьте осторожны, чтобы показать реальный исходный код или что-то, что вы тестировали и выдает тот же симптом