2016-04-14 60 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

相關問題