douyeke2695 2016-01-24 07:45
浏览 1072
已采纳

如何知道表是否为空?

I'm developing a custom management system with php & mysql and till now I have made the admin panel for inserting posts into the site template but I want to show the users that if the table of blog_posts is empty ,echo out empty for example & if not then retrieve the data of table & show them to users.

Here's my blog_posts.php page which shows the posts of the table to users:

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

And this my php_parsers.php which I have included it:

$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'];
}

When I have not inserted any posts from my admin panel ,the blog_posts page will show this notice:

undefined variable: title ...

How to know whether the table is empty or not & if it's empty do something else instead of printing data!

  • 写回答

3条回答 默认 最新

  • duanquezhan7268 2016-01-24 07:52
    关注
    $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";}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名