Warning message in PHP when not finding records

3

Good day, I have a page that searches for me in a table that I have in mysql, the question is .. If I do not find any record that I have to put code and where to get a notice like "No records found?"

I enclose the code that performs the search to mysql (busq.php)

 <?php
 error_reporting(E_ALL ^ E_NOTICE);
 $dia=$_POST['dia'];
 $mes=$_POST['mes'];
 $año=$_POST['año'];

 if(isset($_POST['submit'])){

 $con->real_query("SELECT * FROM entrada WHERE dia like '%$dia%' and mes 
 LIKE '%$mes%' and anio like '%$año%'");
 $resultado= $con->use_result();

 while($muestra=$resultado->fetch_assoc()){
 echo'<tr>';
 echo'<td>'.$muestra['dia'].'</td>';
 echo'<td>'.$muestra['mes'].'</td>';
 echo'<td>'.$muestra['anio'].'</td>'; 
 echo'</tr>';
 }
 }
 ?>

And this is the code where the search form is located:

 <title>busqueda</title>
 <p>
 <form name="form1" method="POST" action="busqueda.php" id="resultado" 
 onKeypress="if(event.keyCode == 13) event.returnValue = false;">
 <h2>Buscar Usuario</h2>
 <input name="dia" type="text" id="busqueda" placeholder="Ingrese el día">
 <input name="mes" type="text" id="mesqueda" placeholder="Ingrese el mes">
 <input name="año" type="text" id="añoqueda" placeholder="Ingrese el año">
 <br><br>
 <input type="submit" name="submit" value="Buscar"> 
 <a href="reporte.php" target="_blank">Ir al reporte</a>
 </p>
 </form>
 <table width="500" border="1" id="tab">
 <tr>
    <td width="60">Día</td>
    <td width="90">Mes</td>
    <td width="90">Año</td>
 </tr>

 <?php
 include('conex.php');
 ?>
<?php
include('busq.php');
?>

</table

I thank you in advance for any help !!

    
asked by julian vargas 26.03.2018 в 20:00
source

3 answers

3

You can try num_rows to verify if the query returns any record.

?php
 error_reporting(E_ALL ^ E_NOTICE);
 $dia=$_POST['dia'];
 $mes=$_POST['mes'];
 $año=$_POST['año'];

 if(isset($_POST['submit'])){

   $query=$con->prepare("SELECT * FROM entrada WHERE dia like ? and mes 
   LIKE ? and anio like ?");
   $query->bind_param('sss', '%'.$dia.'%', '%'.$mes.'%', '%'.$año.'%');
   $resultado= $query->execute();

   if($resultado->num_rows > 0){
     while($muestra=$resultado->fetch_assoc()){
       echo'<tr>';
       echo'<td>'.$muestra['dia'].'</td>';
       echo'<td>'.$muestra['mes'].'</td>';
       echo'<td>'.$muestra['anio'].'</td>'; 
       echo'</tr>';
     }
   } else{
       echo'<tr>';
       echo'<td colspan=3>No se ha encontrado ningun registro</td>'; 
       echo'</tr>';
   }
 }
 ?>
    
answered by 26.03.2018 / 20:14
source
2

I do not identify what driver you use but if it is mysqli for example you can have a syntax similar to the following one, which thanks to the mysqli_num_rows () method, will give you the number of records; it is clear that there should only be a comparison if it is equal to 0 has no records, otherwise there are records

    if($resultado->mysqli_num_rows > 0)
{
    while($muestra=$resultado->fetch_assoc())
     {
         echo'<tr>';
         echo'<td>'.$muestra['dia'].'</td>';
         echo'<td>'.$muestra['mes'].'</td>';
         echo'<td>'.$muestra['anio'].'</td>'; 
         echo'</tr>';
     }
} else {
    echo "Nada de registros";
 }

Being mysqli if I'm not wrong, both:

$resultado->mysqli_num_rows > 0;

or also

$resultado->num_rows > 0;
    
answered by 26.03.2018 в 20:08
2

Reading the documentation for the class mysqli PHP, it seems that there is no row counter for the combination real_query() + use_result() . Instead, the documentation suggests using the affected_rows , which in the case of statements SELECT would perform the same functions as the classic num_rows .

$con->real_query("SELECT * FROM entrada WHERE dia like '%$dia%' and mes 
 LIKE '%$mes%' and anio like '%$año%'");
 $resultado= $con->use_result();

if ($con->affected_rows > 0)  // aqui no estoy segura si se aplica sobre $con o $resultado, tendrías que probarlo.
{
 while($muestra=$resultado->fetch_assoc()){
 echo'<tr>';
 echo'<td>'.$muestra['dia'].'</td>';
 echo'<td>'.$muestra['mes'].'</td>';
 echo'<td>'.$muestra['anio'].'</td>'; 
 echo'</tr>';
 }
}
else
{
 print"No hay coincidencias.";
}
    
answered by 26.03.2018 в 20:23