使用关键字搜索并在表php中显示具有该关键字的所有名称

时间:2016-10-19 04:05:16

标签: php html mysqli

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

因此,一旦我进入上一页的searching_word文本框,它就会显示与searching_word匹配的酒店名称列表。比如说当我搜索Omni时,它需要搜索到的但是只显示

找不到与搜索关键词Omni

相关的酒店

在我的表中,它的数据库是与search_word相匹配的酒店名称

2 个答案:

答案 0 :(得分:0)

您可以尝试替换

if($result == searched_word){

通过

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

答案 1 :(得分: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>