警告:sqlsrv_num_rows()期望参数1为resource, boolean



我对编程有点陌生,我在PHP中使用SQLSRV进行查询,但当导出到Excel时没有给我结果,我抛出错误

警告:sqlsrv_num_rows()期望参数1为

中给定的资源布尔值

这是代码,SQL查询本身在SQL Server中执行,但这里的Excel给我的错误,和互联网搜索和搜索,但找不到答案,这是代码:

<?php
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="Movimientos Cancelados del Mes.xls"');
header('Cache-Control: max-age=0');
$server = "server";
$info = array("Database"=>"DB","UID"=>"USR","PWD"=>"" );
$conn = sqlsrv_connect($server, $info);
$param = array('ReturnDatesAsStrings'=> true);
$opt = array("Scrollable" => SQLSRV_CURSOR_KEYSET);
$per = $_GET["periodo"];
$eje = $_GET["ejercicio"];
$mov = 'Movimiento';
$est = 'Estatus';
$cli = 'Cliente';
$rfc = 'RFC';
$tot = 'Total';
$fec = 'Fecha Timbrado';
$uuid = 'UUID';
$cert = 'Certificado SAT';
$sql = "select v.MovID as '$mov',v.Estatus as '$est',v.Cliente as '$cli',cte.rfc as '$rfc',(v.Importe+v.Impuestos)as '$tot', c.FechaTmibrado as '$fec', c.UUID as '$uuid',c.noCertificadoSAT as '$cert'
from Venta V join CFD c on v.MovID = c.MovID join cte on v.cliente = cte.cliente 
where V.Estatus = 'Cancelado' and c.Periodo = '$per' and c.Ejercicio = '$eje' and  c.Empresa = 'MGJ' 
order by FechaEmision";
$query = sqlsrv_query($conn, $sql);
$campos = sqlsrv_num_rows($query);
$i = 0;
echo "<table border=''><tr>";
echo "<th>$mov</th>";
echo "<th>$est</th>";
echo "<th>$cli</th>";
echo "<th>$rfc</th>";
echo "<th>$tot</th>";
echo "<th>$uuid</th>";
echo "<th>$cert</th>";
while ($i<$campos) {
    echo "<td>".sqlsrv_get_field($query,$i);
    echo "</td>";
    $i++;
}
    echo "</tr>";
while($row=sqlsrv_fetch_array($query)){
    echo "<tr>";
    for ($j=0; $j < $campos; $j++) { 
        echo "<td>".$row[$j]."</td>";
    }
    echo "</tr>";
}
echo "</table>";
   sqlsrv_close( $conn);
print_r(sqlsrv_errors(),true);
?>

警告:sqlsrv_num_rows()期望参数1为

中给定的资源布尔值

表示查询没有返回资源对象,因为查询编译或运行失败。

要查看发生这种情况的原因,可以使用sqlsrv_errors()

if( ($errors = sqlsrv_errors() ) != null) {
    foreach( $errors as $error ) {
        echo "SQLSTATE: ".$error[ 'SQLSTATE']."<br />";
        echo "code: ".$error[ 'code']."<br />";
        echo "message: ".$error[ 'message']."<br />";
    }
}

最新更新