Check if returned records in select PHP

0

I need to check if my 'Select', is returning records to then load the data on the screen. Here is the code I'm 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 code above 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 registration yet."

The goal is: When you have no records, you do not return any errors.

Note: when you have registered data it runs perfectly

    
asked by anonymous 01.03.2018 / 14:20

2 answers

0
<?php
$total = mysqli_num_rows($sql); 
if($total === 0)
{
    echo 'Ainda não existe registro cadastrado';
    exit();
}
?>
    
01.03.2018 / 14:29
0

With an if constructor you can do.

$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;
    
01.03.2018 / 15:21