I have the following code, it is bringing me an error from the query:
sqlsrv_query() expects parameter
andsqlsrv_fetch_array() expects parameter
Although the query is long, what the code does is simply draw a table based on the parameters that my php page receives through the GET method.
The enters the conditional, but generates an error in the query
<table class='table' id='tabla'>
<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE);
$v1 = $_GET['selectVal'];
$v2 = $_GET['fInicial'];
$v3 = $_GET['fFinal'];
$v4 = $_GET['busqueda'];
if($v1 == 1 && $v2 != null && $v3 != null && $v4 != null)
{
echo("
<thead>
<th>Teléfono</th>
<th>Cédula</th>
<th>Beneficiario</th>
<th>Tarjeta</th>
<th>Cliente</th>
<th>Producto</th>
<th>Fecha de Consulta</th>
</thead>
");
$consulta = "
declare @Fecha_ini datetime
declare @Fecha_fin datetime
set @Fecha_ini = '$v2'
set @Fecha_fin = '$v3'
Select * From
(Select top 10000000
CONCAT('0',SUBSTRING(NumeroTelefono,3,3),'-',SUBSTRING(NumeroTelefono,6,3),'-',SUBSTRING(NumeroTelefono,9,12)) AS NumeroTelefono,
CONCAT(SUBSTRING(Cedula,1,2),'.',SUBSTRING(Cedula,3,3),'.',SUBSTRING(Cedula,6,6)) AS Cedula,
CONCAT(TarjetahabienteCliente.PrimerNombre,' ',TarjetahabienteCliente.PrimerApellido) AS Nombre,
NumeroTarjeta as Tarjeta,
tarjetas.cliente as Cliente,
Producto.nombre as Producto,
Convert(varchar(10),FechaConsulta,103) as Fecha
from Operaciones.RegistroConsultaSaldoSMS
inner join tarjetas on tarjetas.Numero = RegistroConsultaSaldoSMS.NumeroTarjeta
inner join Operaciones.TarjetahabienteCliente on TarjetahabienteCliente.id = Tarjetas.TarjetahabienteId
inner join Producto on Producto.id = tarjetas.Producto
where LEN(Cedula) = '8' and replace(cedula,'.','') like '%$v4%' and FechaConsulta between @Fecha_ini and (@Fecha_fin + 1)
order by RegistroConsultaSaldoSMS.id asc) T
union all
Select * From
(Select top 10000000
CONCAT('0',SUBSTRING(NumeroTelefono,3,3),'-',SUBSTRING(NumeroTelefono,6,3),'- ',SUBSTRING(NumeroTelefono,9,12)) AS NumeroTelefono
,CONCAT(SUBSTRING(Cedula,1,1),'.',SUBSTRING(Cedula,3,3),'.',SUBSTRING(Cedula,5,5)) AS Cedula
,CONCAT(TarjetahabienteCliente.PrimerNombre,' ',TarjetahabienteCliente.PrimerApellido) AS Nombre
,NumeroTarjeta as Tarjeta
,tarjetas.cliente as Cliente
,Producto.nombre as Producto
,Convert(varchar(10),FechaConsulta,103) as Fecha
from Operaciones.RegistroConsultaSaldoSMS
inner join tarjetas on tarjetas.Numero = RegistroConsultaSaldoSMS.NumeroTarjeta
inner join Operaciones.TarjetahabienteCliente on TarjetahabienteCliente.id = Tarjetas.TarjetahabienteId
inner join Producto on Producto.id = tarjetas.Producto
where LEN(Cedula) = '7' and replace(cedula,'.','') like '%$v4%' and FechaConsulta between @Fecha_ini and (@Fecha_fin + 1)
order by RegistroConsultaSaldoSMS.id asc) T1
union all
Select * From
(Select top 10000000
CONCAT('0',SUBSTRING(NumeroTelefono,3,3),'-',SUBSTRING(NumeroTelefono,6,3),'-',SUBSTRING(NumeroTelefono,9,12)) AS NumeroTelefono
,CONCAT(SUBSTRING(Cedula,1,3),'.',SUBSTRING(Cedula,4,6)) AS Cedula
,CONCAT(TarjetahabienteCliente.PrimerNombre,' ',TarjetahabienteCliente.PrimerApellido) AS Nombre
,NumeroTarjeta as Tarjeta
,tarjetas.cliente as Cliente
,Producto.nombre as Producto
,Convert(varchar(10),FechaConsulta,103) as Fecha
from Operaciones.RegistroConsultaSaldoSMS
inner join tarjetas on tarjetas.Numero = RegistroConsultaSaldoSMS.NumeroTarjeta
inner join Operaciones.TarjetahabienteCliente on TarjetahabienteCliente.id = Tarjetas.TarjetahabienteId
inner join Producto on Producto.id = tarjetas.Producto
where LEN(Cedula) = '6' and replace(cedula,'.','') like '%$v4%' and FechaConsulta between @Fecha_ini and (@Fecha_fin + 1)
order by RegistroConsultaSaldoSMS.id asc) T2
union all
Select * From
(Select top 10000000
CONCAT('0',SUBSTRING(NumeroTelefono,3,3),'-',SUBSTRING(NumeroTelefono,6,3),'-',SUBSTRING(NumeroTelefono,9,12)) AS NumeroTelefono
,CONCAT(SUBSTRING(Cedula,1,2),'.',SUBSTRING(Cedula,3,5)) AS Cedula
,CONCAT(TarjetahabienteCliente.PrimerNombre,' ',TarjetahabienteCliente.PrimerApellido) AS Nombre
,NumeroTarjeta as Tarjeta
,tarjetas.cliente as Cliente
,Producto.nombre as Producto
,Convert(varchar(10),FechaConsulta,103) as Fecha
from Operaciones.RegistroConsultaSaldoSMS
inner join tarjetas on tarjetas.Numero = RegistroConsultaSaldoSMS.NumeroTarjeta
inner join Operaciones.TarjetahabienteCliente on TarjetahabienteCliente.id = Tarjetas.TarjetahabienteId
inner join Producto on Producto.id = tarjetas.Producto
where LEN(Cedula) = '5' and replace(cedula,'.','') like '%$v4%' and FechaConsulta between @Fecha_ini and (@Fecha_fin + 1)
order by RegistroConsultaSaldoSMS.id asc) T3
union all
Select * From
(Select top 10000000
CONCAT('0',SUBSTRING(NumeroTelefono,3,3),'-',SUBSTRING(NumeroTelefono,6,3),'-',SUBSTRING(NumeroTelefono,9,12)) AS NumeroTelefono
,CONCAT(SUBSTRING(Cedula,1,1),'.',SUBSTRING(Cedula,2,4)) AS Cedula
,CONCAT(TarjetahabienteCliente.PrimerNombre,' ',TarjetahabienteCliente.PrimerApellido) AS Nombre
,NumeroTarjeta as Tarjeta
,tarjetas.cliente as Cliente
,Producto.nombre as Producto
,Convert(varchar(10),FechaConsulta,103) as Fecha
from Operaciones.RegistroConsultaSaldoSMS
inner join tarjetas on tarjetas.Numero = RegistroConsultaSaldoSMS.NumeroTarjeta
inner join Operaciones.TarjetahabienteCliente on TarjetahabienteCliente.id = Tarjetas.TarjetahabienteId
inner join Producto on Producto.id = tarjetas.Producto
where LEN(Cedula) = '4' and replace(cedula,'.','') like '%$v4%' and FechaConsulta between @Fecha_ini and (@Fecha_fin + 1)
order by RegistroConsultaSaldoSMS.id asc) T4
union all
Select * From
(Select top 10000000
CONCAT('0',SUBSTRING(NumeroTelefono,3,3),'-',SUBSTRING(NumeroTelefono,6,3),'-',SUBSTRING(NumeroTelefono,9,12)) AS NumeroTelefono
,Cedula AS Cedula
,CONCAT(TarjetahabienteCliente.PrimerNombre,' ',TarjetahabienteCliente.PrimerApellido) AS Nombre
,NumeroTarjeta as Tarjeta
,tarjetas.cliente as Cliente
,Producto.nombre as Producto
,Convert(varchar(10),FechaConsulta,103) as Fecha
from Operaciones.RegistroConsultaSaldoSMS
inner join tarjetas on tarjetas.Numero = RegistroConsultaSaldoSMS.NumeroTarjeta
inner join Operaciones.TarjetahabienteCliente on TarjetahabienteCliente.id = Tarjetas.TarjetahabienteId
inner join Producto on Producto.id = tarjetas.Producto
where LEN(Cedula) = '3' and replace(cedula,'.','') like '%$v4%' and FechaConsulta between @Fecha_ini and (@Fecha_fin + 1)
order by RegistroConsultaSaldoSMS.id asc) T5
union all
Select * From
(Select top 10000000
CONCAT('0',SUBSTRING(NumeroTelefono,3,3),'-',SUBSTRING(NumeroTelefono,6,3),'-',SUBSTRING(NumeroTelefono,9,12)) AS NumeroTelefono
,Cedula AS Cedula
,CONCAT(TarjetahabienteCliente.PrimerNombre,' ',TarjetahabienteCliente.PrimerApellido) AS Nombre
,NumeroTarjeta as Tarjeta
,tarjetas.cliente as Cliente
,Producto.nombre as Producto
,Convert(varchar(10),FechaConsulta,103) as Fecha
from Operaciones.RegistroConsultaSaldoSMS
inner join tarjetas on tarjetas.Numero = RegistroConsultaSaldoSMS.NumeroTarjeta
inner join Operaciones.TarjetahabienteCliente on TarjetahabienteCliente.id = Tarjetas.TarjetahabienteId
inner join Producto on Producto.id = tarjetas.Producto
where LEN(Cedula) = '2' and replace(cedula,'.','') like '%$v4%' and FechaConsulta between @Fecha_ini and (@Fecha_fin + 1)
order by RegistroConsultaSaldoSMS.id asc) T6
union all
Select * From
(Select top 10000000
CONCAT('0',SUBSTRING(NumeroTelefono,3,3),'-',SUBSTRING(NumeroTelefono,6,3),'-',SUBSTRING(NumeroTelefono,9,12)) AS NumeroTelefono
,Cedula AS Cedula
,CONCAT(TarjetahabienteCliente.PrimerNombre,' ',TarjetahabienteCliente.PrimerApellido) AS Nombre
,NumeroTarjeta as Tarjeta
,tarjetas.cliente as Cliente
,Producto.nombre as Producto
,Convert(varchar(10),FechaConsulta,103) as Fecha
from Operaciones.RegistroConsultaSaldoSMS
inner join tarjetas on tarjetas.Numero = RegistroConsultaSaldoSMS.NumeroTarjeta
inner join Operaciones.TarjetahabienteCliente on TarjetahabienteCliente.id = Tarjetas.TarjetahabienteId
inner join Producto on Producto.id = tarjetas.Producto
where LEN(Cedula) = '1' and replace(cedula,'.','') like '%$v4%' and FechaConsulta between @Fecha_ini and (@Fecha_fin + 1)
order by RegistroConsultaSaldoSMS.id asc) T7
";
$ejecutar = sqlsrv_query($con,$consulta);
$i = 0;
while($fila = sqlsrv_fetch_array($ejecutar))
{
$NumeroTelefono = $fila['NumeroTelefono'];
$Cedula = $fila['Cedula'];
$Nombre = $fila['Nombre'];
$NumeroTarjeta = $fila['Tarjeta'];
$Cliente = $fila['Cliente'];
$Producto = $fila['Producto'];
$FechaConsulta = $fila['Fecha'];
$i++;
echo("<tbody>
<td>$NumeroTelefono</td>
<td>$Cedula</td>
<td>$Nombre</td>
<td>$NumeroTarjeta</td>
<td>$Cliente</td>
<td>$Producto</td>
<td>$FechaConsulta</td>
</tbody>
"
);
}
}
?>
</table>
I have already verified , the data and the variables and I am sure that it works
correctly.
I am seeing that the error could possibly be due to bad concatenation of the HTML AND PHP elements
I would like to know if I am correct and if I am, can you help me correct that concatenation error
Of course if it is another error with its explanation and the code please.
Thanks in advance.
The error
An invalid parameter was passed to sqlsrv_query...
almost always occurs because you pass it an invalid connection, so you have to rule out that possibility and any others by writing controlled code.For example:
Whenever you program you must anticipate all eventualities. It is a mistake to program thinking that things will always go well. The server may be down, there may be a network problem, several things can happen that must be reflected in the code, showing adequate messages for those cases. As a tip, avoid displaying internal database messages and such, change them to custom messages that inform the user. Error control in programs is of paramount importance.