I am a bit new to programming, I have a query in PHP with SQLSRV but when exporting to Excel does not give me the result, I throw the error
Warning: sqlsrv_num_rows () Expects parameter 1 to be resource, boolean given in
this is the code, the SQL query itself executes in SQL Server, but here the Excel gives me that error, and internet search and search but can not find an answer, this is the code:
<?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);
?>