【发布时间】:2016-04-11 02:31:07
【问题描述】:
编辑1:
我实际上并没有得到如下所述的空数组。相反,由于以下异常,我得到了一个空的响应正文:
DBD::ODBC::st fetchrow_array failed: st_fetch/SQLFetch (long truncated DBI attribute LongTruncOk not set and/or LongReadLen too small) (SQL-HY000) [state was HY000 now 01004]
我可以看到有关于的帖子。我会看看那些,看看我能不能在我的身上解决这个问题。如果不成功将编辑。
首先让我先说,我一点也不了解 Perl。这可能是一个粗心的错误——我希望是这样。我正在从一个从 SQL 或前端的 JavaScript 返回的数组和哈希中的一个键构建一个哈希,“short-desc”需要具有下面代码中将来自的值SQL 数据库。
BFHHOTH 15x24S/S +2 UP-HNDWHL-UNSPOKED-GALV 15"x24" flush escape hatch w/hinge, internal handwheel, T-handle on top steel cover and ring
但是使用代码(从开关中删除了不必要的情况):
#!perl
use Switch;
use DBI;
use JSON;
use CGI qw /param/;
use CGI::Carp qw(fatalsToBrowser);
use IO::Compress::Gzip qw(gzip $GzipError);
use URI::Encode;
use URI::Escape;
my $gzip_ok;
my $accept_encoding = $ENV{HTTP_ACCEPT_ENCODING};
if ( $accept_encoding && $accept_encoding =~ /\bgzip\b/ ) {
# $gzip_ok = 1;
}
print "Content-Type: application/json\n";
if ($gzip_ok) {
print "Content-Encoding: gzip\n";
}
print "\n";
my $action = param('ACTION');
my %jsonData;
my @jsonArray;
my $azDSN = DBI->connect('dbi:ODBC:Driver={SQL Server Native Client 10.0};Server=myServer;Database=myDB;Uid=me;Pwd={myPass};Encrypt=yes;Connection Timeout=30;');
switch ($action) {
case "GETINFO" {
my $paramID = param('ID');
getInfo($paramID);
my $json_text = JSON->new->pretty->utf8->encode( \@jsonArray );
if ($gzip_ok) {
my $zipText;
gzip \$json_text, \$zipText,
or die "gzip failed: $GzipError\n";
print $zipText;
}
else {
print $json_text;
}
}
}
sub getInfo {
my $myID = $_[0];
my $statement = <<"SQL";
SELECT
trefQuoteItemsID,
quote_position,
description,
comments
FROM
myDB.dbo.myTable where tID = $myID;
SQL
my $sti = $azDSN->prepare($statement) or die $statement;
$sti->execute() or die $DBI::errstr;
while ( my @row = $sti->fetchrow_array ) {
my %tempData;
%tempData = (
"tref" => $row[0],
"position" => $row[1],
"short_desc" => $row[2],
"comments" => $row[3]
);
$jsonArray[$count] = {%tempData};
$count++;
}
}
一个空数组在前端返回给我。
奇怪的是,如果字符串是:
BFHHOTH 15x24S/S +2 UP-HNDWHL-UNSPOKED-
数组包含正确的对象。 但如果字符串为空,则再次为空:
BFHHOTH 15x24S/S +2 UP-HNDWHL-UNSPOKED-G
也尝试过使用字符串:
qwertyuiopasdfghjklzxcvbnm1234567890qwe #length is 39
让哈希被构建并且:
qwertyuiopasdfghjklzxcvbnm1234567890qwer #length is 40
这将返回一个空数组,因此不会构建哈希。
有没有 Perl 大师有什么建议?
【问题讨论】:
-
请提供一个最小的、可运行的问题演示。
-
这里是创建minimal reproducible example的方法。
-
@ikegami 正在处理这个问题,对此感到抱歉
-
你肯定需要阅读bobby-tables.com来了解SQL注入
-
@glennjackman 谢谢,sql 新手,这是我从别人那里继承的工作,所以代码不是真正的“我的”,所以我一直担心这样的问题。感谢您的建议