使用关键字进行搜索,并在表php中显示包含该关键字的所有名称


search using a keyword and display all names that have that keyword in a table php

    <HTML>

<?php
include 'dbconfig.php';
$hotelname=$_GET['Hotel'];
$con = mysqli_connect($dbhostname, $dbusername, $dbpassword, $dbname) 
    or die("<br>Cannot connect to database!'n");
$query = "SELECT * FROM Hotel WHERE hotelname LIKE  '%".$hotelname."%'";
$result = mysqli_query($con, $query);
if($result) {
    echo "Brodley, Matt";
    echo "<b> </b>";
    echo "You are searching keywords: xxxx";
    echo "<TABLE BORDER=1>";
    echo "<tr><TD>hotelname<TD>city'n";
    while($row = mysqli_fetch_array($result)){
        $hotelname= $row['hotelname'];
        $city= $row['city'];
        echo "<tr><TD>$hotelname<TD>$city'n";
        }
    echo "</TABLE>";
}else echo "no hotel found for search keyword xxxx";
mysqli_free_result($result);
mysqli_close($con);
?>

</HTML>

因此,一旦我在上一页上输入searched_word文本框,它应该显示与searched_word匹配的酒店名称列表。比如当我搜索Omni时它取searched_word但只显示

搜索关键词Omni没有找到酒店

在我的表中,它的数据库their是一个酒店名称,匹配searched_word

您可以尝试替换

if($result == searched_word){

if($result){ // if(mysqli_num_rows($result) > 0)

您可以尝试下面的代码。这里我添加了语句来打印mysql查询错误,并在子查询中删除了"street"列。

<HTML>
        <?php
       include 'dbconfig.php';
       $con = mysqli_connect($dbhostname, $dbusername, $dbpassword, $dbname) 
        or die("<br>Cannot connect to database!'n");
        $query = "SELECT hotelname,city FROM Hotel WHERE city IN ( SELECT city FROM Branch WHERE hotelname LIKE '$_GET[searched_word]')";
        $result = mysqli_query($con, $query);
        if($result){
            echo "Brodley, Matt";
            echo "You are searching keywords: $_GET[searched_word]";
            echo "<TABLE BORDER=1>";
            echo "<tr><TD>hotelname<TD>city'n";
            while($row = mysqli_fetch_array($result)){
                $hotelname= $row['hotelname'];
                $city= $row['city'];
                echo "<tr><TD>$hotelname<TD>$city'n";
                }
            echo "</TABLE>";
        }
        else {
            echo "no hotel found for search keyword $_GET[searched_word]";
            printf("Errormessage: %s'n", mysqli_error($con));
        }
        mysqli_free_result($result);
        mysqli_close($con);
        ?>
        </HTML>