Create a search filter using prepared statements (this checks which fields are empty or not in the search form and adds the variable representing that field as a AND
for the query SQL
, so that it builds the query: "SELECT * FROM sanciones WHERE DATO AND DATO1 AND DATO2"
).
I'd like you to show a message when there are no results, but I do not know where to place the conditional to evaluate that amount of found results.
Try the following:
1) "SELECT * , COUNT(*) FROM sanciones"
, the problem almost the same as the solution Nro 4 , only shows one record, and find hundreds that meet the search conditions, since that statement is not correct in SQL.
2) The 2nd solution involves building the SQL like this: "SELECT dato1,dato2,dato3,daton, count(*) FROM sanciones GROUP by DATO1"
perform the moifaciion and it's something like this:
"SELECT COUNT(*)
, id_sancion
, unidad
, cedula_sancionado
, cedula_sancionador
, cedula_superior
, articulo_falta
, aparte_falta
, documento_seleccion
, fecha_inicio
, fecha_termino
, dias_sancion
, aclaratoria_sancion
, estado_sancion_id
, fecha_proceso
, medida_id
, articulo_circunstancias
, agravante_seleccion
, atenuante_seleccion
, total_demerito
, estado_lugar_id FROM sanciones";
The same error in the Nro1 solution shows only 1 result so they are hundreds.
If I add GROUP BY dato
I get an error function call_user_func_array()
because she expects an array and if I'm wrong this becomes a single value
3) If I compare the variable $resultados
: it will give error since it is a Object Mysqli
4) If I compare $datos = $resultados->fetch_assoc();
that was the most functional I got, it will tell me that if there is data, but once I compare it the function will be executed and I will "spend" so to speak, a record.
Try placing this code
$datos = $resultados->fetch_assoc();
if ($datos < 0) {
echo '<br /> NO HAY RESULTADOS';
}
while ($datos = $resultados->fetch_assoc()){}
But remember that once you call $ data the program is executed and it is already "spent" so to speak a record, so it would then show a result less.
That is, if I add that, it works but it always shows a record less than the expected result.
If the query was:
OMD: 2
OMD: 3
OMD: 5
That meet the search conditions.
The While
will only show OMD:3
and OMD:5
This is the code:
<?php
//Variables//
$numero = $_POST["numero"];
$unidad = $_POST["unidad"];
$cedula1 = $_POST["cedula1"];
$cedula2 = $_POST["cedula2"];
$fechaini = $_POST["fecha_inicio"];
$fechater = $_POST["fecha_termino"];
$tipomedida = $_POST["medida"];
$estadosancion = $_POST["estado"];
//Constantes//
$query = "SELECT * FROM sanciones";
$identificador = "";
$valor = "";
$condiciones = array();
$identificadores = array();
$valores = array();
//Condiciones
if ($numero != "")
{
$condiciones[] = "id_sancion=?";
$identificadores[] = "i";
$valores[] = "$numero";
}
if ($unidad != "")
{
$condiciones[] = "unidad=?";
$identificadores[] = "s";
$valores[] = "$unidad";
}
if ($cedula1 != "")
{
$condiciones[] = "cedula_sancionado=?";
$identificadores[] = "i";
$valores[] = "$cedula1";
}
if ($cedula2 != "")
{
$condiciones[] = "cedula_sancionador=?";
$identificadores[] = "i";
$valores[] = "$cedula2";
}
if ($fechaini != "")
{
$condiciones[] = "fecha_inicio=?";
$identificadores[] = "s";
$valores[] = "$fechaini";
}
if ($fechater != "")
{
$condiciones[] = "fecha_termino=?";
$identificadores[] = "s";
$valores[] = "$fechater";
}
if ($tipomedida != "")
{
$condiciones[] = "medida_id=?";
$identificadores[] = "i";
$valores[] = "$tipomedida";
}
if ($estadosancion != "")
{
$condiciones[] = "estado_sancion_id=?";
$identificadores[] = "i";
$valores[] = "$estadosancion";
}
$query_final = $query;
if (count($condiciones) > 0)
{
$query_final.= " WHERE " . implode(' AND ', $condiciones);
}
$identificadores_final = $identificador;
if (count($identificadores) > 0)
{
$identificadores_final.= implode("", $identificadores);
}
$valores_final = $valor;
if (count($valores) > 0)
{
$valores_final.= implode(',', $valores);
}
//Referencia para meter los ARRAYS en el bind_param
$n = count($valores);
for ($i = 0; $i < $n; $i++)
{
$valores_ref[$i] = & $valores[$i];
}
//Ejecutando la sentencia finial
$stmtx = $conexion->prepare($query_final);
call_user_func_array(array($stmtx,"bind_param") , array_merge(array($identificadores_final) , $valores));
$stmtx->execute();
$resultados = $stmtx->get_result();
while ($datos = $resultados->fetch_assoc())
{
}
?>
I would like to count the number of records that will come out and then if it gives 0 to show a message