簡體   English   中英

PHP 5.4 到 7.4 mssql 到 sqlsrv sql 查詢未運行

[英]PHP 5.4 to 7.4 mssql to sqlsrv sql query not running

一個小前言 我是一年級的系統管理員,我一直在遷移服務器以使這家公司保持最新狀態。 我遇到了運行 php 5.4 的服務器,我正在嘗試將其移至 php 7.4。 一切最初都是用 mssql 編寫的,我將其移至 sqlsrv。 我有連接工作。 我可以成功地執行一些查詢,但是我無法讓這些為 mssql 編寫的查詢與 sqlsrv 一起使用。

我知道的事情:

  • $record作為我用 gettype 測試過的數組返回。
  • $conn之所以有效,是因為我可以通過簡單的查詢從數據庫中查詢表。

sql 查詢在 sql 服務器中正確運行。

任何建議都將不勝感激,因為我覺得我需要重寫整個腳本,因為我已經為此苦苦掙扎了幾天。 下面的代碼片段只是內置在if else鏈中的眾多查詢之一。

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;
        }
    }


我已經開始使用PDO ,但是我的一台服務器仍然使用sqlsrv函數。 當我從mssql函數轉換時,我遇到了同樣的麻煩。

您可以使用sqlsrv_next_result function(如上面的提示)移動指針,或者您可以簡單地提供一個附加參數來強制將結果作為關聯數組返回。 我發現標准的while循環像舊的 function 那樣工作,當它這樣做時。

代替:

while ($record = sqlsrv_fetch_array($result))

利用:

while ($record = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM