Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database 在字符串eq中使用未初始化值$end_time1…….perl中出现错误_Database_Perl_Null - Fatal编程技术网

Database 在字符串eq中使用未初始化值$end_time1…….perl中出现错误

Database 在字符串eq中使用未初始化值$end_time1…….perl中出现错误,database,perl,null,Database,Perl,Null,我想从我的数据库中检索一些数据,如果$end_time1为空,则将更新表,否则将不执行任何操作,但当我运行代码时,我发现$end_time1为空,则将更新表,但如果不为空,则将返回错误: Use of uninitialized value $end_time1 in string eq ........ 我的部分代码: my $select_sth = $dbh->prepare("SELECT id ,H1, H2, addr1, addr2, time_1, time_2,

我想从我的数据库中检索一些数据,如果$end_time1为空,则将更新表,否则将不执行任何操作,但当我运行代码时,我发现$end_time1为空,则将更新表,但如果不为空,则将返回错误:

Use of uninitialized value $end_time1 in string eq ........
我的部分代码:

my $select_sth = $dbh->prepare("SELECT id ,H1, H2, addr1, addr2, time_1, time_2,   
end_time_1,end_time_2,count1,count2 FROM service") or die "$dbh->errstr";
$select_sth->execute() or die "$dbh->errstr";
while (my @row_ref = $select_sth->fetchrow_array)
 {
    my $Rid = $row_ref[0];
    my $addr1 = $row_ref[3];
    my $addr2 = $row_ref[4];
    my $end_time1 = "NULL" unless $row_ref[7];
    my $end_time2 = "NULL" unless $row_ref[8];
    my $count1 = $row_ref[9];
    my $count2 = $row_ref[10];
    if($end_time1 eq "NULL")
     {
        print "$end_time1 is null\n";
        $dbh->do ("update service set end_time_1 = '$datetime_now' where id = $Rid");
     }
 }
请告诉某人我的代码有什么问题?如何修复?

使用:


如果未定义
$row\u ref[7]
,则当前代码仅设置
$endtime1
。这意味着如果
$row\u ref[7]
确实有一个值,那么
$endtime1
是未定义的,因此当您测试它时,
会在字符串等式中使用未初始化的值$end\u time1..
错误

更改您的代码,以便将
$endtime1
设置为
$row\u ref[7]
(如果已定义)或
NULL

    my $end_time1 = $row_ref[7] || 'NULL';
然后可以使用现有代码:

if ($end_time1 eq "NULL")
{
    print "end_time1 is null\n";
    $dbh->do ("update service set end_time_1 = '$datetime_now' where id = $Rid");
}

$endtime2
也存在同样的问题,因此您可能需要进行类似的更改。

我希望在end\u time1为null时打印,但无论end\u time1是否为null,每次都会打印您的代码
if ($end_time1 eq "NULL")
{
    print "end_time1 is null\n";
    $dbh->do ("update service set end_time_1 = '$datetime_now' where id = $Rid");
}