Jean Lima
Jean Lima

Reputation: 93

SQL-Server Select returning null on PHP

I have a query that returns all the data while running at MSSQL, but when I try to get the result with php code it returns null

SELECT:

   $query =  "SELECT  DISTINCT (E080SER.desser) as desser,
                E080SER.CODFAM codfam, e085cli.apecli apecli, 
                E085CLI.CODCLI codcli, E085CLI.NOMCLI nomeCli
                FROM
                E160CTR,
                E160CVS, e080ser,
                E085CLI,
                E070EMP,
                E070FIL
                WHERE
                e070emp.nomemp like '%Gestão tech%' and 
                e080ser.codser = e160cvs.codser and
                e080ser.codser like ('%manw%') and (E160CTR.CODEMP = 1)  and
                ((E160CTR.CODEMP = E070FIL.CODEMP) AND (E160CTR.CODFIL = 
                E070FIL.CODFIL) AND  
                (E160CTR.CODCLI = E085CLI.CODCLI) AND (E160CVS.CODEMP = 
                E160CTR.CODEMP) AND 
                (E160CVS.CODFIL = E160CTR.CODFIL) AND (E160CVS.NUMCTR = 
                E160CTR.NUMCTR)) AND
                (E160CTR.SITCTR = 'A') and e080ser.sitser = 'a' and 
                E080SER.CODEMP IN (1, 9)
                  order by e080ser.desser";

PHP CODE:

 $sql = sqlsrv_query($conn, $query);
 while($item = sqlsrv_fetch_array($sql)){
    var_dump($item);
}

Upvotes: 0

Views: 903

Answers (3)

Jean Lima
Jean Lima

Reputation: 93

I've found the problem

The problem was the encoding, I put the $query inside of utf8_encode(), and now it is returning the results.

Thank you all for your time.

Upvotes: 0

Zhorov
Zhorov

Reputation: 30023

Sometimes it's necessary to fetch all result sets with sqlsrv_next_result() to get your data. You may try with this:

<?php
...
$sql = sqlsrv_query($conn, $query);
do {
   while($item = sqlsrv_fetch_array($sql)){
      var_dump($item);
   }
} while (sqlsrv_next_result($sql));
...

?>

Upvotes: 2

Andrey Nikolov
Andrey Nikolov

Reputation: 13460

There is an extra semicolon after the while loop, i.e. the body of the loop is empty. Then the result you try to read is after the last row, that's why you don't get what you expected.

Upvotes: 1

Related Questions