0
  • Table: student
    enter image description here

  • student.php

    <?php
    function findStudentRecord()
    {
          //Db connection
    
          $q1 = "select * from student where gender = 'F'";
          $r1 = mysqli_query($dbc, $q1);
          $total_records = mysqli_num_rows($r1);
    
          $record = array();
          if($total_records > 0)
          {
              while($row1 = mysqli_fetch_array($r1, MYSQLI_ASSOC))
              {
                  $record[] = $row1;
              }
          }
          else
          {
              //[HERE]
          }
    
          return $record;
    }
    
    $record = findStudentRecord();
    //[HERE]
    ?>
    
  • I want to find female student record but there is no record from my database. How do I return 0 value from function and display "No record found" on my web page in [HERE] section?

3
  • ´return 0;´ and then use a if sentence to determine the record? or, return "No record found"; Commented Nov 10, 2015 at 12:10
  • use if statement to return "No return found" Commented Nov 10, 2015 at 12:10
  • Possible duplicate of PHP if/else statement Commented Nov 10, 2015 at 12:56

2 Answers 2

2
if($total_records > 0)

add no else block so it will return an empty array now you can do something like this

$records = findStudentRecord();
if(count($records) === 0) {
    echo "No record found";
}
Sign up to request clarification or add additional context in comments.

Comments

2

I would not change your function. It returns an array (may be empty) in any case wich is quite consistent.

Instead look at the number of array items returned:

$record = findStudentRecord();
//[HERE]
if(count($record) == 0) {
  echo "No record found";
} else {
  // what ever
}

Comments

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.