dqw70970 2016-05-12 15:50
浏览 48

我想在查询没有返回结果时显示消息“找不到结果”

I am using the following code to search in the site..I wish to display the message "no result found" when the query returns no result.What changings should i do in the code

The HTML is:

<div class="form-container">
     <form method="get" action="search.php">
      <input type="text" name="search" placeholder="Search keywords..."/>
      <input type="submit" name="find" value="">
     </form>
    </div>

The PHP is:

<?php
if(isset($_GET['find'])){
 $search = @$_GET['search'];
 $query = "SELECT * FROM `me` WHERE `post_title` LIKE '%$search%'";
 $run = mysqli_query($con,$query);
while($row=mysqli_fetch_assoc($run)):
    $post_id = $row['post_id'];
    $post_category = $row['post_category'];
    $post_author = $row['post_author'];
    $post_title = $row['post_title'];
    $post_content = substr($row['post_content'],0,700);
    $post_video = $row['post_video'];
    $post_misc = $row['post_misc'];
    $post_image = $row['post_image'];
    $post_date = $row['post_date'];
?>


   <a class="post_title" href="complete_post.php?complete1=<?php echo $post_id; ?>">
   <?php echo $post_title; ?>
   </a>
   <p style="color:#949494; font-family:calibri; text-align:justify; margin-left:8px; width:700px; font-size:15pt; float:left;">
   <span style="float:left; font-size:9pt; font-family:arial; color:#959595;">
   By <b><?php echo $post_author; ?></b> on <b><?php echo $post_date; ?></b> in 
   <b><?php echo $post_category; ?></b>
   </span>
   </br>
    <?php echo $post_content; ?>...
   </p>
   <a href="complete_post.php?complete=<?php echo $post_id; ?>" class="r-m">
   Read more..
   </a>
<?php endwhile; } ?> 
  • 写回答

2条回答 默认 最新

  • dtgv52982 2016-05-12 15:54
    关注

    Add an if-condition before your while-loop

    $run = mysqli_query($con,$query);
    if ( ! empty($run) && (! empty($_GET['search'])) ) {
      while($row=mysqli_fetch_assoc($run)):
        // your while code
      endwhile;
    } else {
      echo "Nothing Found";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料