How to check if the number of mysql results is empty?

0

I'm having a hard time displaying a message when the query returns no results. When select returns 1 result "if(mysqli_num_rows($result1) > 0)" is showing correctly, but when it does not return, it does not contain anything ( else ).

Here is 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 } ?>   

    
asked by anonymous 11.12.2017 / 06:22

1 answer

3

Simple, num_rows is within while , your code does not make sense. What the code is doing is ...

  • While there are rows ( while ($reg = mysqli_fetch_array($result1)) ):
    • If there are rows ( mysqli_num_rows($result1) > 0) ):
      • Display form
    • If not:
      • Displays "Expired Link"

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

You can simply ignore while , after all there is LIMIT 1 , this 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, this will work as expected.

    
11.12.2017 / 08:55