我是新來的Php,我得到了這段代碼,如果沒有關於所選類別的文章,我想顯示警告消息。我試過「mysqli_num_row」,但我沒有工作。感謝您從現在開始:))我如何在php中使用「mysqli_num_rows」?
<?php
if(isset($_GET['category'])){
$post_category_id = $_GET['category'];
}
$query = "SELECT * FROM posts WHERE post_category_id = $post_category_id ";
$select_all_posts_query = mysqli_query($connection, $query);
$num_rows = mysqli_num_rows($select_all_posts_query);
if($num_rows.count() < 0){
echo "<h1 class='text-center'>THERE IS NO POST ABOUT THIS CATEGORY!</h1>";
echo "<img style='margin-left:225px;' width='300' class='img-responsive' src='images/sorry.png'/>";
}else{
while($row = mysqli_fetch_assoc($select_all_posts_query)){
$post_id = $row['post_id'];
$post_title = $row['post_title'];
$post_author = $row['post_author'];
$post_date = $row['post_date'];
$post_image = $row['post_image'];
$post_content = substr($row['post_content'], 0, 50);
?>
<h1 class="page-header">
Page Heading
<small>Secondary Text</small>
</h1>
<!-- First Blog Post -->
<h2>
<a href="post.php?p_id=<?php echo $post_id; ?> "><?php echo $post_title ?></a>
</h2>
<p class="lead">
by
<a href="index.php">
<?php echo $post_author ?>
</a>
</p>
<p><span class="glyphicon glyphicon-time"></span>
<?php echo $post_date ?>
</p>
<hr>
<a href="post.php?p_id=<?php echo $post_id; ?>"><img width="600" class="img-responsive" src="images/<?php echo $post_image;?>" alt=""></a>
<hr>
<p>
<?php echo $post_content ?>
</p>
<a class="btn btn-primary" href="#">Read More <span class="glyphicon glyphicon-chevron-right"></span></a>
<hr>
<?php } }?>
這些都是我的代碼。
它是用作'$ NUM_ROWS = mysqli_num_rows($ select_all_posts_query); if($ num_rows> 0){---}' – Saty
只要if(mysqli_num_rows($ select_all_posts_query)> 0)'會起作用 –
'if($ num_rows <0){'只是這個 – devpro