mysql — поиск PHP не работает, если выполняется поиск более 2 ключевых слов

Я пытаюсь построить окно поиска, которое будет искать в 2 столбцах.

HTML

<div class="autocomplete-list1">
<input type="text" class="searchpropertyinputs areaname-list-completed" name="buildingorlocation" id="buildingorlocation" placeholder="Building or Location" onkeyup="autofillbuildingorlocation()" maxlength="50" />
<ul class="areaname-list searchpropertyinputs" id="property_buildingorlocation_list"></ul>
</div>

PHP

$property_buildingorlocation = ($_GET['buildingorlocation']);
$property_buildingorlocation = str_replace(',', ' ', $property_buildingorlocation);$select = $con->prepare(

"SELECT
*
from
tbl_property
WHERE
property_buildingname LIKE '%$property_buildingorlocation%'
OR property_areaname LIKE '%$property_buildingorlocation%'");

$select->setFetchMode(PDO::FETCH_ASSOC);
$select->execute();

Если я введу до двух ключевых слов, таких как Мэдисон-Гарден, то получится результат, но если я поставлю три, как Мэдисон-Гарден-роуд, то результата не будет … Может кто-нибудь просветить меня, пожалуйста …

0

Решение

Попробуйте следующий код:

<?php
$property_buildingorlocations = array_filter(explode(',', $_GET['buildingorlocation']));
$query = "SELECT * from tbl_property ";
$count = count($property_buildingorlocations);
if($count){
$query .=" WHERE ";
}
$i = 0;
foreach ($property_buildingorlocations as $property_buildingorlocation){
$query .= "property_buildingname LIKE '%".trim($property_buildingorlocation)."%'";
if(++$i !== $count) {
$query .=" OR ";
}

}$select = $con->prepare($query);

$select->setFetchMode(PDO::FETCH_ASSOC);
$select->execute();
0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector