[英]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>
So once I enter into the searched_word textbox on the previous page it should display a list of hotelnames that match the searched_word. 因此,一旦我进入上一页的searched_word文本框,它应该显示与searched_word匹配的酒店名称列表。 Say when I search Omni it takes the searched_word but just displays
说当我搜索Omni时,它使用了searched_word,但仅显示
no hotel found for search keyword Omni 找不到搜索关键字Omni的酒店
In my table that its database their is a hotelname that matches the searched_word 在我的表中,其数据库是与searched_word匹配的旅馆名称
you may try replacing 您可以尝试更换
if($result == searched_word){
by 通过
if($result){ // if(mysqli_num_rows($result) > 0)
You can try the below code. 您可以尝试以下代码。 Here I have added the statement to print the mysql query error and also removed "street" column in sub query.
在这里,我添加了打印mysql查询错误的语句,还删除了子查询中的“街道”列。
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.