2

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. Everything was originally written in mssql and I'm moving it to 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.

Things I know:

  • $record is returning as an array I tested that with gettype.
  • $conn works because I can query tables from the DB with a simple query.

The sql queries run correctly in sql server.

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.

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


Zhorov
  • 28,486
  • 6
  • 27
  • 52
Jolly9642
  • 33
  • 4
  • Any errors in the log? "not running" isn't much of a description – ADyson Jan 25 '21 at 17:06
  • 1
    It has been a long time since I've been in MSSQL. I'm seeing two queries, is it possible that the first needs to be skipped over using `sqlsrv_next_result($result)`? You should probably also be checking `sqlsrv_rows_affected($result)` during debugging – Chris Haas Jan 25 '21 at 17:14
  • Chris Haas! That was the problem!! Thanks for the tip I didn't think about them as two separate queries and never knew about sqlsrv_next_result. – Jolly9642 Jan 25 '21 at 19:15
  • Why didn't you switch to PDO? – Dharman Jan 25 '21 at 20:31

1 Answers1

0

I have begun using PDO, but one of my servers still uses the sqlsrv functions. I had the same trouble you are, when I converted from the mssql functions.

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. I discovered that the standard while loop works like the old function did, when it is done that way.

Instead of:

while ($record = sqlsrv_fetch_array($result))

Use:

while ($record = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))
Laura Larsen
  • 26
  • 1
  • 1