dongniaoli1822 2019-04-18 07:27
浏览 17
已采纳

搜索关键字并在未找到结果时抛出“未找到”错误

I want to search a keyword with the use of the MySQL's LIKE clause (for example: LIKE %$keyword%) and then get the result back. If there are no results, i want to show some error message.

I have a table named ads_post and i'm searching for the keyword in the fields named cat_name and city_name.

Could someone help me with this?


search-result.php

<?php 
    if (isset($_GET['submit'])) {
        $keyword = $_GET['keywoed'];
        $query = "select * from ads_post where cat_name like '%$keywoed%' or city_name like '%$keywoed%'";
        $result = mysqli_query($conn, $sql);

        if (mysqli_num_rows($result) > 0) {
            while ($row = mysqli_fetch_assoc($result)) {
                echo "cat Name: " . $row["cat_name"] . "<br>";
            }
        } else {
            echo "0 results";
        }

        mysqli_close($conn);
    } 
?>

HTML form code

<form action="search-result.php" method="get" >
    <input class="form-control"  name="keyword" placeholder="Search any keyword..?">
    <input type="submit"   value="search">
</form>
  • 写回答

2条回答 默认 最新

  • douzi7890 2019-04-18 07:54
    关注

    i catch something

    $query = "select * from ads_post where cat_name like '%$keywoed%' or city_name like '%$keywoed%'";
            $result = mysqli_query($conn, $sql);
    

    first defined $query but mysqli_query is $sql please replace $query not $sql :)

    $result = mysqli_query($conn, $query);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制