如果表中没有数据,如何在php中显示消息 [英] How to show the message in php if there is no data in table

查看:53
本文介绍了如果表中没有数据,如何在php中显示消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这是一个愚蠢的问题,但是对不起,我是php的乞讨者,实际上我想在表中没有数据的情况下显示该消息,该怎么办.我的代码在这里,谢谢!

i think it's silly question but sorry i'm begginer to php , actually i want to show the message if i've no data in table so how can i do. my code is here Thanks in advance

<?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>

请阅读我的代码并指导我如何显示我的信息,谢谢您

please read my code and guide me how i'll show my message thank you once again

推荐答案

您可以使用 mysqli_num_rows()检查结果中的行数

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

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

这篇关于如果表中没有数据,如何在php中显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆