PHP + Mssql不显示结果

时间:2017-08-09 21:22:27

标签: php angularjs sql-server json

连接到MSSQL

session_start();

// CONEXION

$link = mssql_connect('xxx.xxx.xx.xx', 'xxx', 'xxx');
mssql_select_db("xxx",$link);

if (!$link || !mssql_select_db('BDFlexline', $link)) {
    die('No se puede conectar o seleccionar una base de datos!');
}
// FIN CONEXION

if( isset( $_GET["dcto"] ) ){
    $dcto = $_GET["dcto"];
    $empresa = $_GET['empresa'];
}

在这里查询

$conn ="SELECT 
D.TIPODOCTO, d.CORRELATIVO, d.CLIENTE,coalesce(c.razonsocial,'') RAZONSOCIAL, 
v.DESCRIPCION as VENDEDOR , D.NUMERO, D.FECHA, D.TOTAL, d.Local as CLUB 
from documento D 
left join Vendedor v on d.Empresa = v.EMPRESA and d.Vendedor = v.CODIGO
left join ctacte c on c.empresa=d.empresa and c.tipoctacte=d.tipoctacte and c.ctacte =d.idctacte 
where  d.total >= 0 
and d.aprobacion not in ('S','N') 
and d.tipodocto='".$dcto."' 
and d.empresa='".$empresa."'  
and d.vigencia not in ('N','A') 
and d.Fecha between CONVERT(VARCHAR(25), DATEADD(mm, -1, DATEADD(dd,-(DAY(GETDATE())-1),GETDATE())),105) 
and CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,GETDATE()))),DATEADD(mm,1,GETDATE())),105)
order by d.numero";

查询正常,可以在SQL管理控制台中按预期工作,但是php ...

    $result= mssql_query($conn, $link);

############# PRUEBA 1 #################
    $json = array();     

do {  
    while ($row = mssql_fetch_object($result)){  
        $json[] = $row;     
    }  
}while ( mssql_next_result($result) ); 
echo json_encode($json);
mssql_close($link);  

结果是[],但必须是:

[{"TIPODOCTO":"COTIZACION","CORRELATIVO":"3059","CLIENTE":"20548547-2","RAZONSOCIAL":"SERVICIOS AIR LMTD","VENDEDOR":"Diana","NUMERO":"0000003079","FECHA":{"date":"2017-07-03 00:00:00.000000","timezone_type":3,"timezone":"Europe\/Berlin"},"TOTAL":"3111964.00000000","CLUB":"ADM"},
{"TIPODOCTO":"COTIZACION","CORRELATIVO":"3062","CLIENTE":"71540800-7","RAZONSOCIAL":"UNIVERSIDAD SAC","VENDEDOR":"Ernesto","NUMERO":"0000003482","FECHA":{"date":"2017-07-05 00:00:00.000000","timezone_type":3,"timezone":"Europe\/Berlin"},"TOTAL":"16670624.00000000","CLUB":"ADM"}] .... and more

我需要json格式的结果,因为我正在使用angularJS。 谢谢你的想法

1 个答案:

答案 0 :(得分:0)

我猜测$ dcto或$ empressa的值是错误的,导致空白结果集。对于测试做

echo "conn=$conn<br/>";

并查看屏幕上显示的内容。然后在控制台中运行该查询,并确保它为您提供所需的结果。