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

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

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条)

相关推荐 更多相似问题