我遇到下面的查询方法问题。 我正在尝试搜索数据库中可能匹配的字符串。
但是,使用INSTR(fieldName, '".$q."')
仅返回精确字词或整个字词的匹配项。
例如,如果$q
包含“10th”,它将返回所有具有“10th”的行。
如果$q
包含“第10条街道”,则会返回所有具有“两个字”的行。
但是如果$q
包含来自不同行的两个或更多单词,如“Sales,10th Street”,它将不返回任何内容。这意味着,服务器无法在任何行中找到确切的字符串。
所以,我需要能够返回至少在$q
中匹配的行。
请仔细阅读我的代码并帮我修改,以符合我的目的。
<?php
require_once('../data/conString_mysqli.php');//create connection
//$referrer = 'www.servicehub.ng/';
if ($_SERVER['HTTP_REFERER'] == "../servicehub.ng/find/index.php") {
echo '<script>window.location="../home/?q=no query";</script>';
}
if (isset($_POST['services'])) {
$q = trim($_POST['services']);
} else {
if (isset($_REQUEST['q'])) {
$q = trim($_REQUEST['q']);
} else {
if (isset($_REQUEST['s'])) {
$s = trim($_REQUEST['q']);
} else {
header('Location:../home/');
}
}
}
?>
<div style="background: #f0ad4e;" id="menunavbar">
</div>
<section style="padding: 1.5em 0; background: #fdfdfd;">
<div class="container">
<div class="row">
<div class="col-sm-8 col-sm-offset-2">
<form method="post" action="index.php">
<div class="form-group" style="margin-bottom: 0 !important;">
<div class="input-group">
<input class="form-control selectpicker" id="services" name="services" multiple
data-max-options="1"
placeholder="Enter a keyword to search by location, business type, business name, business items, sellers, market type etc"
style="height:55px;">
<span class="input-group-btn">
<button class="btn btn-default btn-warning" type="submit"
onclick="searchProvider(this.value)">
<i class="fa fa-search" style="margin: 0;"></i>
</button>
</span>
</div>
</div>
</form>
</div>
</div>
</div>
</section>
<section>
<div class="container">
<table width="100%" border="1">
<tr>
<!--<td style="width:2%; border-radius:3px; height:45px; text-align:center; vertical-align:middle; background-color:#FF9933; color:#FFFFFF; font-weight:bold; font-size:small;">S/N</td>-->
<td style="width:12%; border-radius:3px; height:45px; text-align:center; vertical-align:middle; background-color:#FF9933; color:#FFFFFF; font-weight:bold; font-size:small;">
Business Name
</td>
<td style="width:23%; border-radius:3px; height:45px; text-align:center; vertical-align:middle; background-color:#FF9933; color:#FFFFFF; font-weight:bold; font-size:small;">
Business Address
</td>
<td style="width:23%; border-radius:3px; height:45px; text-align:center; vertical-align:middle; background-color:#FF9933; color:#FFFFFF; font-weight:bold; font-size:small;">
Description
</td>
<td style="width:12%; border-radius:3px; height:45px; text-align:center; vertical-align:middle; background-color:#FF9933; color:#FFFFFF; font-weight:bold; font-size:small;">
Contact Name
</td>
<td style="width:10%; border-radius:3px; height:45px; text-align:center; vertical-align:middle; background-color:#FF9933; color:#FFFFFF; font-weight:bold; font-size:small;">
Category
</td>
<td style="width:10%; border-radius:3px; height:45px; text-align:center; vertical-align:middle; background-color:#FF9933; color:#FFFFFF; font-weight:bold; font-size:small;">
Number/ Email
</td>
</tr>
<div class="col-sm-9" id="dispSearch">
<?php
$search_query = mysqli_query($connect,
"SELECT * FROM service_providers WHERE INSTR(pname, '" . $q . "') OR INSTR(bizaddr, '" . $q
. "') OR INSTR(bizname, '" . $q . "') OR INSTR(bizdesc, '" . $q . "') OR INSTR(pmobile, '" . $q
. "') OR INSTR(pemail, '" . $q . "') OR INSTR(bizcat, '" . $q . "') OR INSTR(pmobile, '" . $q
. "')ORDER BY bizname"
);
$records = mysqli_num_rows($search_query);
if (!$records >= 1) {
die('<script type="text/javascript">alert("Could not find any services related to your search. Please try another.");window.location="../home/?q='
. $q . '";</script>');
} else {
while ($row = mysqli_fetch_array($search_query)) {
$pcode = $row['pcode'];
$bizname = $row['bizname'];
$bizaddr = $row['bizaddr'];
$bizdesc = $row['bizdesc'];
$pname = $row['pname'];
$pmobile = $row['pmobile'];
$pemail = $row['pemail'];
if ($row['bizcat'] == '') {
$category = "UNKNOWN CATEGORY";
} else {
$category = $row['bizcat'];
}
?>
<?php
echo '
<tr>
<!--<td style="width:2%; height:45px; text-align:left; vertical-align:middle; border:#FF6633; color:#FF6633; font-weight:500; font-size:smaller;">'
. $pcode . '</td>-->
<td style="width:12%; height:45px; text-align:center; vertical-align:middle; border:#FF6633; color:#FF6633; font-weight:500; font-size:smaller;">'
. $bizname . '</td>
<td style="width:23%; height:45px; text-align:center; vertical-align:middle; border:#FF6633; color:#FF6633; font-weight:500; font-size:smaller;">'
. $bizaddr . '</td>
<td style="width:23%; height:45px; text-align:center; vertical-align:middle; border:#FF6633; color:#FF6633; font-weight:500; font-size:smaller;">'
. $bizdesc . '</td>
<td style="width:12%; height:45px; text-align:center; vertical-align:middle; border:#FF6633; color:#FF6633; font-weight:500; font-size:smaller;">'
. $pname . '</td>
<td style="width:10%; height:45px; text-align:center; vertical-align:middle; border:#FF6633; color:#FF6633; font-weight:500; font-size:smaller;">'
. $category . '</td>
<td style="width:10%; height:45px; text-align:center; vertical-align:middle; border:#FF6633; color:#FF6633; font-weight:500; font-size:smaller;">'
. $pmobile . '; ' . $pemail . '</td>
</tr>
<tr><td> </td></tr>
';
}//end while
}//end if($record)
mysqli_close($connect);
?>
</div>
</table>
</div>
</div>
</section>