How to check if the mysql result number is empty?

Asked

Viewed 185 times

0

I’m having some difficulty displaying a message when the query does not return results. When the select returns 1 result "if(mysqli_num_rows($result1) > 0)" is showing correctly, but when it does not return, it brings nothing (else).

Follow my code below:

<div class="part3">
  <center><h2>Redefina a sua nova senha</h2></center>
  <?php
    if(isset($_GET['hash'])) {
      $hash = $_GET['hash'];

      $sql1 = "SELECT * from hr_link where link = '$hash' limit 1";
      $result1 = mysqli_query($connect, $sql1);
      while($reg = mysqli_fetch_array($result1)){

        $hash2 = $reg['link'];
        $idusuario = $reg['idusuario'];
        if(mysqli_num_rows($result1) > 0) {
    ?>
          <center>
            <form style="max-width: 450px" method="POST" name="formuser" action="cadastrok2.php" class="form-horizontal">
              <input type="password" name="txtnovasenha" placeholder="Nova Senha" class="txtfieldd" />
              <input type="password" name="txtrpt" placeholder="Confirme a nova senha" class="txtfieldd" />
              <input type="hidden" name="txtidusuario" value="<?php echo $idusuario ?>" />
              <br/>
              <center>
                <input type="submit" name="rec_key" value="Salvar" onclick="return validar() "  style="color: white; background: green; padding: 15px; font-size: 18px; border-radius: 5px; margin-top: 20px; cursor: pointer;border: none"/>
              </center>        
            </form>
          </center>  
    <?php 
        } else { ?>
          <center>
            <br/>
              <form style="max-width: 450px" name="loginform" method="post" action="userauth.php?">
                <a href="?id=1" class="btn_modal3"><h5>Link expirou. Clique aqui para gerar novamente</h5></a>
                <br/>
                <center></center>
              </form>
          </center> 
 <?php 
       }
    }
  }else{ 
?>
     <center>
        <form style="max-width: 450px" name="loginform" method="post" action="userauth.php?">
          <a href="?id=1" class="btn_modal3"><h5>Link expirou. Clique aqui para gerar novamente</h5></a>
          <br/>
          <center>
          </center>
        </form>
      </center>
<?php } ?>   

1 answer

3


Simple, the num_rows is inside the while, your code doesn’t make sense. What the code is doing is...

  • As long as there are lines (while ($reg = mysqli_fetch_array($result1))):
    • If there are lines (mysqli_num_rows($result1) > 0)):
      • Aphorical displays
    • If not:
      • Displays "Link has expired"

The while will only work if there is a record (or more), it will never work if there are zero records. Soon, in normal situation, this if will never be used, it will always be > 0. So that the if be called it is necessary that the while is executed, the while will only be executed when there is a line (or more lines).


You can just ignore the while, after all there is the LIMIT 1, that would solve the problem:

<center><h2>Redefina a sua nova senha</h2></center>
<?php

if (isset($_GET['hash'])) {
    $hash = mysqli_real_escape_string($connect, $_GET['hash']);
    $result1 = mysqli_query($connect, "SELECT link, idusuario from hr_link where link = '$hash' LIMIT 1");

    if (mysqli_num_rows($result1) > 0) {
        list($hash2, $idusuario) = mysqli_fetch_row($result1);
        ?>
        <center>
            <form style="max-width: 450px" method="POST" name="formuser" action="cadastrok2.php"
                  class="form-horizontal">
                <input type="password" name="txtnovasenha" placeholder="Nova Senha" class="txtfieldd"/>
                <input type="password" name="txtrpt" placeholder="Confirme a nova senha" class="txtfieldd"/>
                <input type="hidden" name="txtidusuario" value="<?php echo $idusuario ?>"/>
                <br/>
                <center>
                    <input type="submit" name="rec_key" value="Salvar" onclick="return validar() "
                           style="color: white; background: green; padding: 15px; font-size: 18px; border-radius: 5px; margin-top: 20px; cursor: pointer;border: none"/>
                </center>
            </form>
        </center>
        <?php
        exit();
    }
}
?>
<center>
    <br/>
    <form style="max-width: 450px" name="loginform" method="post" action="userauth.php?">
        <a href="?id=1" class="btn_modal3"><h5>Link expirou. Clique aqui para gerar novamente</h5></a>
        <br/>
        <center>
        </center>
    </form>
</center>

Although this is still with other problems, it will work as expected.

  • 1

    Thank you very much, Inkeliz. It worked, it helped me a lot.

Browser other questions tagged

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