2016-01-24 72 views
0

我正在開發一個自定義管理系統,用php & mysql,直到現在我已經做了管理面板插入帖子到網站模板,但我想告訴用戶,如果blog_posts表爲空,回顯出例如&如果不是,則檢索表&的數據將它們展示給用戶。如何知道表是否爲空?

這裏是我的blog_posts.php網頁,其中顯示該表對用戶的帖子:

<div id="box"> 
    <div class="BJadidBold"> 
    <h1><?php echo $title; ?></h1> 
    <p><?php echo $content; ?></p></br> 
    </div> 
</div> 

這我php_parsers.php我已經包括它:

$select_posts = "select * from posts"; 
$run_posts = mysqli_query($con,$select_posts); 
while($row=mysqli_fetch_array($run_posts)){ 
    $id = $row['post_id']; 
    $title = $row['post_title']; 
    $date = $row['post_date']; 
    $author = $row['post_author']; 
    $image = $row['post_image']; 
    $keywords = $row['post_keywords']; 
    $content = $row['post_content']; 
} 

當我沒有從我的管理面板插入任何帖子時,blog_posts頁面將顯示此通知:

未定義的變量:標題...

如何知道表是否爲空或不&如果它是空做其他的事情,而不是打印數據!

回答

0
$select_posts = "select * from posts";  $run_posts = mysqli_query($con,$select_posts); 
if (mysqli_num_rows($run_posts) > 0) {  while($row=mysqli_fetch_array($run_posts)){ 
$id = $row['post_id']; 
$title =  $row['post_title']; 
$date = $row['post_date']; 
$author = $row['post_author']; 
$image = $row['post_image']; 
$keywords = $row['post_keywords']; 
$content = $row['post_content']; } 
}else{ 
echo"No Posts";} 
0

Befor您$ select_posts查詢插入代碼的這些線路

$num_rows=mysqli_query("select count(*)as total from posts ");
$tot_rows=mysqli_fetch_array(total);
if($tot_rows['total']<1){
echo "No Post Found ";
}
else{

Your fetch query here

}

0

使用mysqli_num_rows()查詢表,以確定是否返回任何職位或不後直接。

$totalrows = mysqli_num_rows($run_posts); 

通過'if'語句,您可以爲用戶提供消息或行列表。爲避免錯誤發生,請不要進入'while'語句,直到找到某些內容,否則需要將所有變量設置爲null以避免未定義的警告。

相關問題