2

一个小前言 我是一年级的系统管理员,我一直在迁移服务器以使这家公司保持最新状态。我遇到了运行 php 5.4 的服务器,我正在尝试将其移至 php 7.4。一切最初都是用 mssql 编写的,我将其移至 sqlsrv。我有连接工作。我可以成功地执行一些查询,但是我无法让这些为 mssql 编写的查询与 sqlsrv 一起使用。

我知道的事情:

  • $record以数组的形式返回,我用 gettype 对其进行了测试。
  • $conn有效,因为我可以通过简单的查询从数据库中查询表。

sql 查询在 sql server 中正确运行。

任何建议都将不胜感激,因为我觉得我需要重写整个脚本,因为我已经为此苦苦挣扎了几天。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;
        }
    }


4

1 回答 1

0

我已经开始使用PDO,但我的一台服务器仍在使用这些sqlsrv功能。当我从mssql函数转换时,我遇到了同样的麻烦。

您可以使用函数移动指针sqlsrv_next_result(如上面的提示),或者您可以简单地提供一个附加参数来强制将结果作为关联数组返回。我发现标准while循环像旧函数一样工作,当它以这种方式完成时。

代替:

while ($record = sqlsrv_fetch_array($result))

利用:

while ($record = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))
于 2021-06-09T19:35:35.083 回答