2016-04-14 58 views
4

我认为这是一个愚蠢的问题,但对不起,我是php的成员,实际上我想显示消息,如果我没有数据在表中,所以我怎么能做。我的代码是在这里先谢谢如何在php中显示消息,如果表中没有数据

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

请阅读我的代码,并指导我如何我会告诉我的消息再次

回答

3

你需要检查是否返回行,如果没有,显示错误。

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

说明:

1)我们已经采取了可变$i,初始化为0

2)在while循环中,它被递增。

3)如果我们有记录,while循环后,我们将得到$i多于0

4)如果仍然0小于1,则表示没有记录并显示错误信息。

+0

谢谢亲爱的宝贵意见 –

+0

我的做法绝对没问题。我没有对现有代码做太多的修改。逻辑也是正确的。 OP已将我的答案标记为解决方案。如果我的答案需要改进/更正,欢迎评论。 – Pupil

+0

嗨,瞳孔谢谢。也感谢你对我的问题发表评论的其他朋友。 –

4

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

行数谢谢
$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

1

您可以使用mysqli_num_rows计算行数返回:

返回结果集的行数。

因为,mysqli_num_rows将返回0如果没有结果,你可以用!来检查,如果结果是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 
} 
+0

谢谢亲爱的宝贵意见 –

+0

为什么使用downvote这个答案? – Panda

+0

只需注意您使用'mysqli_num_rows'而不是'mysqli_num_rows($ run)'需要传递'mysqli_query'变量。在你的代码中'mysqli_num_rows'被视为字符串 – Saty

相关问题