2

ちょっとした序文 私は 1 年目のシステム管理者で、この会社を最新の状態にするためにサーバーを移行してきました。PHP 5.4 を実行しているサーバーに遭遇しました。それを PHP 7.4 に移行しようとしています。すべてがもともと mssql で書かれていたので、それを sqlsrv に移動しています。接続が機能しています。一部のクエリは正常に実行できますが、mssql 用に作成されたこれらのクエリを sqlsrv で動作させることができません。

私が知っていること:

  • $recordgettypeでテストした配列として返されます。
  • $conn簡単なクエリでDBからテーブルをクエリできるため、機能します。

SQL クエリは、SQL サーバーで正しく実行されます。

数日間これに苦労しているため、このスクリプト全体を書き直す必要があると感じているため、アドバイスをいただければ幸いです。if else以下のスニペットは、チェーンに組み込まれた多くのクエリの 1 つにすぎません。

PHP 5.4 で書かれた元のコード:

$query = "Select * INTO #temptable FROM (
    SELECT Employee
        ,transdate
        ,sum([RegHrs]) as RegHrs
        ,sum([OvtHrs]) as OvtHrs
    
    FROM [dbo].[tkDetail]
    WHERE TransDate >= cast('" . $startdate . "' as datetime) and TransDate <= cast('" . $enddate . "' as datetime)
    GROUP BY Employee, transdate) as x

    SELECT LastName,FirstName,emmain.Employee, emcom.PayType, cast(data.TransDate as date) as TransDate
    ,data.reghrs
    ,data.ovthrs
    from dbo.emmain emmain
    Left Join #temptable data on emmain.employee = data.employee
    Left Join dbo.EMCompany emcom on emmain.employee = emcom.employee
    Left Join dbo.EmployeeCustomTabFields custom on emmain.employee = custom.employee
    WHERE custom.custFullTimePartTime = 'Full Time' and emcom.status = 'A'
    ";
    $result = mssql_query($query);
    while ( $record = mssql_fetch_array($result) ) {
        // BUGFIX: Salary reporting shows no regular hour entry as null instead of 0.0
        if ($record['reghrs'] == null) {
            $record['reghrs'] = "0.0";
        }
        
        // BUGFIX: Salary reporting shows no overtime hour entry as null instead of 0.0
        if ($record['ovthrs'] == null) {
            $record['ovthrs'] = "0.0";
        }
        
        if (($record['reghrs'] + $record['ovthrs'] <= 4) && ($record['reghrs'] + $record['ovthrs'] > -1)) {
            print "\t\t\t\t\t<tr>\n";
            print "\t\t\t\t\t\t<td>" . $record['Employee'] . "</td>\n";
            print "\t\t\t\t\t\t<td>" . $record['FirstName'] . " " . $record['LastName'] . "</td>\n";
            print "\t\t\t\t\t\t<td>" . $record['PayType'] . "</td>\n";
            print "\t\t\t\t\t\t<td>" . number_format((float) $record['reghrs'], 3) . "</td>\n";
            print "\t\t\t\t\t\t<td>" . number_format((float) $record['ovthrs'], 3) . "</td>\n";
            print "\t\t\t\t\t\t<td>" . $record['TransDate'] . "</td>\n";
            print "\t\t\t\t\t</tr>\n";
            $reccount += 1;
        }
    }

私がやろうとしたこと:

$query = "Select * INTO #temptable FROM (
    SELECT Employee
        ,transdate
        ,sum([RegHrs]) as RegHrs
        ,sum([OvtHrs]) as OvtHrs
    
    FROM [dbo].[tkDetail]
    WHERE TransDate >= cast('" . $startdate . "' as datetime) and TransDate <= cast('" . $enddate . "' as datetime)
    GROUP BY Employee, transdate) as x

    SELECT LastName,FirstName,emmain.Employee, emcom.PayType, cast(data.TransDate as date) as TransDate
    ,data.reghrs
    ,data.ovthrs
    from dbo.emmain emmain
    Left Join #temptable data on emmain.employee = data.employee
    Left Join dbo.EMCompany emcom on emmain.employee = emcom.employee
    Left Join dbo.EmployeeCustomTabFields custom on emmain.employee = custom.employee
    WHERE custom.custFullTimePartTime = 'Full Time' and emcom.status = 'A'
    ";

    $result = sqlsrv_query($conn, $query);
    if( $result ) {
        echo "result true";
    }else{
        echo "result false <br />";
        die( print_r( sqlsrv_errors(), true));
    }    
    while ($record = sqlsrv_fetch_array($result)) 
        {
            
        // BUGFIX: Salary reporting shows no regular hour entry as null instead of 0.0
        if ($record["reghrs"] == null) {
            $record["reghrs"] = "0.0";
        }
        
        
        // BUGFIX: Salary reporting shows no overtime hour entry as null instead of 0.0
        if ($record['ovthrs'] == null) {
            $record['ovthrs'] = "0.0";
        }
        
        if (($record['reghrs'] + $record['ovthrs'] <= 4) && ($record['reghrs'] + $record['ovthrs'] > -1)) {
            print "\t\t\t\t\t<tr>\n";
            print "\t\t\t\t\t\t<td>" . $record['Employee'] . "</td>\n";
            print "\t\t\t\t\t\t<td>" . $record['FirstName'] . " " . $record['LastName'] . "</td>\n";
            print "\t\t\t\t\t\t<td>" . $record['PayType'] . "</td>\n";
            print "\t\t\t\t\t\t<td>" . number_format((float) $record['reghrs'], 3) . "</td>\n";
            print "\t\t\t\t\t\t<td>" . number_format((float) $record['ovthrs'], 3) . "</td>\n";
            print "\t\t\t\t\t\t<td>" . $record['TransDate'] . "</td>\n";
            print "\t\t\t\t\t</tr>\n";
            $reccount += 1;
        }
    }


4

1 に答える 1