Hello World
Hello World

Reputation: 63

How to determine if while loop returns nothing from db in php?

I would like to know how can I determine that the while loop that retrieves data from MySQL returns nothing. Here's my code, please take a look at it:

    <?php 
function getAdmins(){
    global $con;
    global $id;
    $get_admin = "select * from admins where id != $id";
    $run_admin = mysqli_query($con,$get_admin);
    while($row_admin = mysqli_fetch_array($run_admin)){
        $id_admin = $row_admin['id'];
        $username_admin = $row_admin['username'];
        $fname_admin = $row_admin['fname'];
        $lname_admin = $row_admin['lname'];
        $email_admin = $row_admin['email'];
        echo '....';
            }
}

Upvotes: 1

Views: 1803

Answers (5)

Aniruddha Chakraborty
Aniruddha Chakraborty

Reputation: 1867

Count rows using mysqli_num_rows($run_admin);

<?php 

function getAdmins(){
    global $con;
    global $id;
    $get_admin = "select * from admins where id != $id";
    $run_admin = mysqli_query($con,$get_admin);

    if (mysqli_num_rows($run_admin) > 0) {

      while($row_admin = mysqli_fetch_array($run_admin)){
          $id_admin = $row_admin['id'];
          $username_admin = $row_admin['username'];
          $fname_admin = $row_admin['fname'];
          $lname_admin = $row_admin['lname'];
          $email_admin = $row_admin['email'];
          echo '....';
              }

         }
    } else {
       echo 'empty data';
     }

}

Upvotes: 2

Jon Story
Jon Story

Reputation: 3051

$run_admin is just a mysqli_result object, and will be empty if there is nothing returned...

So we can use the following

if(empty($run_admin))
{
    //Nothing returned
}

Or alternately, mysqli_num_rows() does basically the same thing, but explicitly looks at the $num_rows property of the mysqli_result object

if(mysqli_num_rows($run_admin) == 0)
{
    //Nothing returned
}

The latter is probably clearer as it is very obvious that we're saying "if nothing has been returned" rather than "if there is no result" - although the outcome of both is the same, mysqli_num_rows is more descriptive if someone reads the code later.

Upvotes: 0

Rene Korss
Rene Korss

Reputation: 5484

Detect how many rows you found.

$get_admin = "select * from admins where id != $id";
$run_admin = mysqli_query($con, $get_admin);

// Count of rows found
$num_rows = mysqli_num_rows($run_admin);

if($num_rows > 0){
    while($row_admin = mysqli_fetch_array($run_admin)){
        $id_admin = $row_admin['id'];
        $username_admin = $row_admin['username'];
        $fname_admin = $row_admin['fname'];
        $lname_admin = $row_admin['lname'];
        $email_admin = $row_admin['email'];
        echo '....';
    }
}
else{
    // Nothing returned
}

Upvotes: 0

KiwiJuicer
KiwiJuicer

Reputation: 1972

$results = array();

while($row_admin = mysqli_fetch_array($run_admin)){
    results[] = $row_admin;
}

if (empty($results)) {
    echo 'Empty, no results';
}

Upvotes: 0

Keilo
Keilo

Reputation: 986

Before entering the while loop set $loopExecuted = false; Then inside the loop set $loopExecuted = true; After the loop you can check the variable to see if there was at least one iteration of the while loop.

Upvotes: 3

Related Questions