我一直在一个网站上使用这个搜索功能,它从发布的词中提取词并搜索数据库。我对其进行了调整,以便它可以接受来自网站另一部分的通过 GET 发送给它的内容。我遇到的问题是当您向它发送多个词时,例如 searchstock=some search words 它不会起作用。如果我只发送单个单词,例如使用 GET 的 searchstock=word,它工作得很好,只有当发送多个单词时,它才不起作用,只会显示数据库中的所有结果。奇怪的是,通过 POST 进行多词搜索效果很好。
// Begin Search Section >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
function search($db) {
if (isset($_POST['searchstock'])){$words = $_POST['searchstock'];}
if (isset($_GET['searchstock'])){$words = $_GET['searchstock'];}
$searchQuery = ''; // search query is empty by default
$searchCondition = "(cultivar LIKE '%%' OR description LIKE '%%' OR species LIKE '%%' OR colour LIKE '%%')";
$searchFieldName = 'cultivar'; // name of the field to be searched
$searchFieldName2 = 'description';
$searchFieldName3 = 'species';
$searchFieldName4 = 'colour';
if(isset($_POST['searchstock']))
{ // check if a query was submitted
$searchQuery = trim($_POST['searchstock']); // getting rid of unnecessary white space
$searchTerms = explode(" ", $searchQuery); // Split the words
$searchCondition = "($searchFieldName LIKE '%" . implode("%' OR $searchFieldName LIKE '%", $searchTerms) . "%')"; // Forming the condition for the sql
$searchCondition .= " OR ($searchFieldName2 LIKE '%" . implode("%' OR $searchFieldName2 LIKE '%", $searchTerms) . "%')";
$searchCondition .= " OR ($searchFieldName3 LIKE '%" . implode("%' OR $searchFieldName3 LIKE '%", $searchTerms) . "%')";
$searchCondition .= " OR ($searchFieldName4 LIKE '%" . implode("%' OR $searchFieldName4 LIKE '%", $searchTerms) . "%')";
}
else if(isset($_GET['searchstock']))
{ // check if a query was submitted
$searchQuery = trim($_GET['searchstock']); // getting rid of unnecessary white space
$searchTerms = explode(" ", $searchQuery); // Split the words
$searchCondition = "($searchFieldName LIKE '%" . implode("%' OR $searchFieldName LIKE '%", $searchTerms) . "%')"; // Forming the condition for the sql
$searchCondition .= " OR ($searchFieldName2 LIKE '%" . implode("%' OR $searchFieldName2 LIKE '%", $searchTerms) . "%')";
$searchCondition .= " OR ($searchFieldName3 LIKE '%" . implode("%' OR $searchFieldName3 LIKE '%", $searchTerms) . "%')";
$searchCondition .= " OR ($searchFieldName4 LIKE '%" . implode("%' OR $searchFieldName4 LIKE '%", $searchTerms) . "%')";
}
// the rest is just database connection and retrieving the results
$sql = <<<SQL
SELECT * FROM stock WHERE $searchCondition;
SQL;
if(!$result = $db->query($sql)){ die('There was an error running the query [' . $db->error . ']');}
while($row = $result->fetch_assoc()){ //Show your results here eg: $row['field1']
$searchid = $row['id'];
$searchgenusid = $row['genusid'];
// End Search
// Search DB Function Start //
$sql4 = <<<SQL
SELECT * FROM `stock` WHERE `id` = '$searchid';
SQL;
if(!$stockresult = $db->query($sql4)){ die('There was an error running the query [' . $db->error . ']');}
while($stockrow = $stockresult->fetch_assoc()){
最佳答案
function search($db) {
$words=''
if (isset($_POST['searchstock'])){$words = $_POST['searchstock'];}
if (isset($_GET['searchstock'])){$words = $_GET['searchstock'];}
if (!empty($words){
$searchQuery = ''; // search query is empty by default
$searchCondition = "(cultivar LIKE '%%' OR description LIKE '%%' OR species LIKE '%%' OR colour LIKE '%%')";
$searchFieldName = 'cultivar'; // name of the field to be searched
$searchFieldName2 = 'description';
$searchFieldName3 = 'species';
$searchFieldName4 = 'colour';
$searchQuery = trim(words); // getting rid of unnecessary white space
$searchTerms = explode(" ", $searchQuery); // Split the words
$searchCondition = "($searchFieldName LIKE '%" . implode("%' OR $searchFieldName LIKE '%", $searchTerms) . "%')"; // Forming the condition for the sql
$searchCondition .= " OR ($searchFieldName2 LIKE '%" . implode("%' OR $searchFieldName2 LIKE '%", $searchTerms) . "%')";
$searchCondition .= " OR ($searchFieldName3 LIKE '%" . implode("%' OR $searchFieldName3 LIKE '%", $searchTerms) . "%')";
$searchCondition .= " OR ($searchFieldName4 LIKE '%" . implode("%' OR $searchFieldName4 LIKE '%", $searchTerms) . "%')";
// the rest is just database connection and retrieving the results
$sql = <<<SQL
SELECT * FROM stock WHERE $searchCondition;
SQL;
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
while($row = $result->fetch_assoc()){
$searchid = $row['id'];
$searchgenusid = $row['genusid'];
//if its work on _POST then should also work on _GET
//do what ever you want
}
}
}
关于php - 搜索功能不适用于多个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34485092/