4

I have a report, which has a few prompts, and based on the prompt selection, sometimes there might be no records in the report. In such cases, I want to show a message NO RECORDS FOUND on the Cover Page.

Can anyone tell me how I do this?

<?php      
error_reporting(0);
include("connection.php");
session_start();
if(!($_SESSION['email']))
{
    echo "please login first to access this page";
    header("refresh:3;url=index.php");
    exit();
}
?>
<!DOCTYPE html>
<html>
<head>
    <style>
        body
        {
            margin: 0;
            padding: 0;
        }
        table
        {
            border-collapse: collapse;
            width: 100%;
            font-family: sans-serif;
            font-size: 15px;
        }
        th,td
        {
            padding: 5px;
            text-align: left;
        }
        tr:nth-child(even)
        {
            background: #edf0f5;
        }
        th
        {
            background: #00A800;
            color: white;
            padding: 5px;
            font-family: sans-serif;
        }
        a
        {
            text-decoration: none;
            color: #00A800;
        }
        a:hover
        {
            text-decoration: underline;
        }
    </style>
    <title>View all the data</title>
</head>
<body>
    <table>
        <tr>
            <th>Roll NO</th>
            <th>Student Name</th>
            <th>Father Name</th>
            <th>Class</th>
            <th>Class Section</th>
            <th>Phone number</th>
            <th>Email Address</th>
            <th>Address</th>
            <th>Edit</th>
            <th>View Fees</th>
            <th>Attendance</th>
        </tr>
        <?php
        $select="select *from student where class='1' AND section='B' ";
        $run=mysqli_query($con,$select);
        $i=0;
        while($row=mysqli_fetch_array($run))
        {
            $id=$row['id'];
            $s_name=$row['s_name'];
            $f_name=$row['f_name'];
            $class=$row['class'];
            $section=$row['section'];
            $phone=$row['phone'];
            $email=$row['email'];
            $address=$row['address'];
             $i++;
        ?>
        <tr>
            <td><?php echo $i;?></td>
            <td><?php echo $s_name;?></td>
            <td><?php echo $f_name;?></td>
            <td><?php echo $class;?></td>
            <td><?php echo $section;?></td>
            <td><?php echo $phone;?></td>
            <td><?php echo $email;?></td>
            <td><?php echo $address;?></td>
            <td><a href="edit.php?edit=<?php echo $id;?>" target="_blank">Edit</a></td>
            <td><a href="view_fees.php?view_fees=<?php  echo $id;?>" target="_blank">Fees</a></td>
            <td><a href="attendance.php" target="_blank">Attendance</a></td>
        </tr>
        <?php
        }
        ?>            
    </table>
</body>
</html>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Fawad Shah
  • 309
  • 2
  • 11

3 Answers3

4

You can use mysqli_num_rows() to check the number of rows in a result

$select = "select *from student where class='1' AND section='B' ";
$run = mysqli_query($con, $select);
if (mysqli_num_rows($run > 0)) {// check if your query return result
    // your code
} else {
    echo "NO result found";
}

Read http://php.net/manual/en/mysqli-result.num-rows.php

Saty
  • 22,443
  • 7
  • 33
  • 51
3

You need to check if rows are returned, if not, show error.

$i = 0;
while($row=mysqli_fetch_array($run)) {
 // Your code
 $i++;
}
if ($i <1) {
?>
<tr><td colspan="11">There are no records.</td></tr>
<?php
}

Explanation:

1) We have taken a variable $i, initialized to 0

2) In while loop, it is incremented.

3) If we have records, after while loop, we will get $i more than 0.

4) If it is still 0 that is less than 1, it means there are no records and show the error message.

Pupil
  • 23,834
  • 6
  • 44
  • 66
  • My approach is absolutely fine. I did not make too many too many changes to existing code. Logic is also correct. OP has marked my answer as solution. If my answer needs improvements/corrections, comments are welcome. – Pupil Apr 14 '16 at 11:52
  • We may have different approaches for same solution. If I did not use `mysqli_num_rows()`, it does not mean, my answer is incorrect. – Pupil Apr 14 '16 at 11:56
1

You can use mysqli_num_rows to count the number of rows returned:

Returns the number of rows in the result set.

Since, mysqli_num_rows will return 0 if there's no result, you can use ! to check if the result is false.

$select="select *from student where class='1' AND section='B' ";
$run=mysqli_query($con, $select);
if (!mysqli_num_rows($run)) {
    echo "No result is found";
} else {
    // to do if there's result
}
Panda
  • 6,955
  • 6
  • 40
  • 55
  • Just notice you are using `mysqli_num_rows` instead `mysqli_num_rows($run)` need to pass `mysqli_query` variable. In your code `mysqli_num_rows` is treated as string – Saty Apr 14 '16 at 12:08