1

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 '....';
            }
}
4
  • Do you mean if mysqli_fetch_array($run_admin) does not return? Returns an array of strings that corresponds to the fetched row or NULL if there are no more rows in resultset. Commented Dec 11, 2015 at 15:22
  • I mean it gets nothing from db .. for example lets say my table is empty at all. How can I show that? Commented Dec 11, 2015 at 15:24
  • Man , you misunderstood ... I meaned no result found my query because my table is empty Commented Dec 11, 2015 at 15:26
  • You just check that there is at least one row with results before you fetch them: if (mysqli_num_rows($run_admin)) { /* do while-loop with fetch here */ } Commented Dec 11, 2015 at 15:30

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Alternatively do your database call before entering the loop. And check the data to see if the loop will execute.
Although this does work, why would you not use mysqli_num_rows - there's no need to manually set a flag when the number of results is known
2

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';
     }

}

Comments

0
$results = array();

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

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

3 Comments

Well, usually you need an array anyway, so why using mysqli_num_rows
It makes for easier and cleaner code to use mysqli_num_rows(). There's no need to complicate things when there are tools available for that exact need.
@KiwiJuicer - because mysqli_result already contains an array, you're just copying an array (which is slow) for no reason! mysqli_result already has a property $num_rows, which we can use for this. mysqli_num_rows just retrieves that number (which has already been calculated) for virtually no performance cost.
0

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
}

Comments

0

$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.

2 Comments

if(mysqli_num_rows($run_admin) = 0) missing an extra = ;-)
Whoops, I've been working in VB all morning, thanks :p

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.