dongsu4345
2019-01-09 18:21
浏览 20
已采纳

SQL-Server选择在PHP上返回null

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);
}
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanguan1573 2019-01-09 20:13
    已采纳

    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.

    打赏 评论
  • doufang2023 2019-01-09 19:24

    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.

    打赏 评论
  • doudi2005 2019-01-09 20:08

    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));
    ...
    
    ?>
    
    打赏 评论

相关推荐 更多相似问题