简体   繁体   English

PHP 5.4 到 7.4 mssql 到 sqlsrv sql 查询未运行

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

A little preface I'm a year one system administrator and I've been migrating servers to get this company up to date.一个小前言 我是一年级的系统管理员,我一直在迁移服务器以使这家公司保持最新状态。 I've run into a server running php 5.4 and I'm trying to move it to php 7.4.我遇到了运行 php 5.4 的服务器,我正在尝试将其移至 php 7.4。 Everything was originally written in mssql and I'm moving it to sqlsrv.一切最初都是用 mssql 编写的,我将其移至 sqlsrv。 I have the connection working.我有连接工作。 I can do some queries successfully, but I can't get these queries that were written for mssql to work with sqlsrv.我可以成功地执行一些查询,但是我无法让这些为 mssql 编写的查询与 sqlsrv 一起使用。

Things I know:我知道的事情:

  • $record is returning as an array I tested that with gettype. $record作为我用 gettype 测试过的数组返回。
  • $conn works because I can query tables from the DB with a simple query. $conn之所以有效,是因为我可以通过简单的查询从数据库中查询表。

The sql queries run correctly in sql server. sql 查询在 sql 服务器中正确运行。

Any advice would be appreciated because I'm feeling like I'm going to need to rewrite this whole script because I've been struggling with this for a few days now.任何建议都将不胜感激,因为我觉得我需要重写整个脚本,因为我已经为此苦苦挣扎了几天。 The snippet below is just one of many queries built into an if else chain.下面的代码片段只是内置在if else链中的众多查询之一。

Original code written in php 5.4: 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;
        }
    }

What I've tried to do:我试图做的事情:

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


I have begun using PDO , but one of my servers still uses the sqlsrv functions.我已经开始使用PDO ,但是我的一台服务器仍然使用sqlsrv函数。 I had the same trouble you are, when I converted from the mssql functions.当我从mssql函数转换时,我遇到了同样的麻烦。

You can move the pointer with the sqlsrv_next_result function (as the tip above mentions), or you can simply provide an additional argument to force the results to be returned as an associative array.您可以使用sqlsrv_next_result function(如上面的提示)移动指针,或者您可以简单地提供一个附加参数来强制将结果作为关联数组返回。 I discovered that the standard while loop works like the old function did, when it is done that way.我发现标准的while循环像旧的 function 那样工作,当它这样做时。

Instead of:代替:

while ($record = sqlsrv_fetch_array($result))

Use:利用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM