Build a search filter using prepared statements (it checks which fields are empty or not in the search form and adds the variable that represents that field as a AND
to the query SQL
, so it builds the query: "SELECT * FROM sanciones WHERE DATO AND DATO1 AND DATO2"
).
I would like it to show a message when there are no results, but I don't know where to put the conditional to evaluate the amount of results found.
Try the following:
1) "SELECT * , COUNT(*) FROM sanciones"
, the problem is almost the same as solution No. 4 , it only shows one record, so they find hundreds that meet the search conditions, since that sentence 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"
make the moifaciion and it looks 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 of solution # 1 shows only 1 result so they find hundreds.
If I add the function, GROUP BY dato
the function gives me an error call_user_func_array()
because it expects an array and if I'm wrong this becomes a single value
3) If I compare the variable $resultados
: it will give an error since it is a Mysqli Object
4) If I compare $datos = $resultados->fetch_assoc();
what was the most functional thing I got, it will tell me that there is data, but once I compare it, the function will be executed and I will "spend" a record, so to speak.
Try putting this code
$datos = $resultados->fetch_assoc();
if ($datos < 0) {
echo '<br /> NO HAY RESULTADOS';
}
while ($datos = $resultados->fetch_assoc()){}
But let's remember that once you call $data, the program is executed and a record is already "spent", so to speak, so later it would show one less result.
That is, if I add that, it works but it always shows one record less than the expected result.
If the query gave:
OMD: 2
OMD: 3
OMD: 5
That meet the search conditions.
He While
will only show OMD:3
andOMD: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 show a message
The variable
$resultados
is an object of the typemysqli_result
and has a property callednum_rows
which:That is to say that if
$resultados->num_rows
, is equal tocero
(0
), then you should print "There are no records"Solution: