Check if you returned records in select PHP

Asked

Viewed 5,173 times

1

I need to check if my 'Select', is returning records to then load the data on the screen. Below follows the code I am using:

$sql = mysqli_query($conn,"SELECT * FROM USUARIOS;");

$linhas=mysqli_num_rows($sql); 

//eu preciso verificar aqui!!!!!

while ($linhas = mysqli_fetch_array($sql, MYSQLI_ASSOC)) {
   echo "<td hidden='true'>". $linhas['id'] ."</td>";
   echo "<td>". $linhas['nome'] ."</td>";
} 

Based on the above code how should I check if the query returned records?

Because if you have not returned, I will display a message on the screen, "There is no registered record yet".

The goal is: When you have no records, no error returns.

Obs.: when you have registered data wheel perfectly

2 answers

1


<?php
$total = mysqli_num_rows($sql); 
if($total === 0)
{
    echo 'Ainda não existe registro cadastrado';
    exit();
}
?>
  • Thank you very much friend!!!! It worked out right.!!

  • @Paulogalego mark the answer as useful please since it solved your problem !

0

With a builder if da para fazer.

$sql = mysqli_query($conn,"SELECT * FROM USUARIOS;");
$linhas=mysqli_num_rows($sql); 
if( $linhas > 0 ) :
    echo".<h5>."$linhas registros encontrados."</h5>";
    while ($linhas = mysqli_fetch_array($sql, MYSQLI_ASSOC)) {

       echo "<td>". $linhas['nome'] ."</td>";
    } 
else:
    echo"Ainda não existe registros !";
endif;
  • Thanks for the help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.