php - PHP - 在数据库中搜索多个单词
问题描述
我有一个搜索功能,但只有当我用一个词搜索时才显示结果,如果我输入 2 个词并没有显示任何内容。是否可以使用多个单词进行搜索?我希望能够像“house new york”一样进行搜索,并使用该搜索词向我显示我的数据库中的数据。我的 HTML:
<form name='frmSearch' action='' method='post'>
<div style='text-align:right;margin:20px 0px;'><input type='text' name='search[keyword]' value="<?php echo $search_keyword; ?>" id='keyword' maxlength='25'></div>
<table class='tbl-qa'>
<thead>
<tr>
<th class='table-header' width='10%'>ID Anunt</th>
<th class='table-header' width='10%'>Titlu</th>
<th class='table-header' width='10%'>Camere</th>
<th class='table-header' width='10%'>Etaj</th>
<th class='table-header' width='10%'>Compartimentare</th>
</tr>
</thead>
<tbody id='table-body'>
<?php
if(!empty($result)) {
foreach($result as $row) {
?>
<tr class='table-row'>
<td><?php echo $row['id_anunt']; ?></td>
<td><?php echo $row['titlu']; ?></td>
<td><?php echo $row['camere']; ?></td>
<td><?php echo $row['etaj']; ?></td>
<td><?php echo $row['compartimentare']; ?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
<?php echo $per_page_html; ?>
</form>
我的搜索功能:
<?php
$search_keyword = '';
if(!empty($_POST['search']['keyword'])) {
$search_keyword = $_POST['search']['keyword'];
}
$sql = 'SELECT * FROM apartament where titlu LIKE :keyword OR camere LIKE :keyword OR etaj LIKE :keyword OR compartimentare LIKE :keyword ORDER BY titlu ASC';
/* Pagination Code starts */
$per_page_html = '';
$page = 1;
$start=0;
if(!empty($_POST["page"])) {
$page = $_POST["page"];
$start=($page-1) * ROW_PER_PAGE;
}
$limit=" limit " . $start . "," . ROW_PER_PAGE;
$pagination_statement = $connect->prepare($sql);
$pagination_statement->bindValue(':keyword', '%' . $search_keyword . '%', PDO::PARAM_STR);
$pagination_statement->execute();
$row_count = $pagination_statement->rowCount();
if(!empty($row_count)){
$per_page_html .= "<div style='text-align:center;margin:20px 0px;'>";
$page_count=ceil($row_count/ROW_PER_PAGE);
if($page_count>1) {
for($i=1;$i<=$page_count;$i++){
if($i==$page){
$per_page_html .= '<input type="submit" name="page" value="' . $i . '" class="btn-page current" />';
} else {
$per_page_html .= '<input type="submit" name="page" value="' . $i . '" class="btn-page" />';
}
}
}
$per_page_html .= "</div>";
}
$query = $sql.$limit;
$pdo_statement = $connect->prepare($query);
$pdo_statement->bindValue(':keyword', '%' . $search_keyword . '%', PDO::PARAM_STR);
$pdo_statement->execute();
$result = $pdo_statement->fetchAll();
解决方案
推荐阅读
- sql - SQL 将查询结果中的多行插入到现有表的单个列中
- rust - 如何调用具有多个签名者的 solana 程序
- swift - NSToolbarItem 图像在运行的应用程序中看起来很糟糕,但在界面生成器中却没有
- ibmhttpserver - 进行后端调用时由 IHS 验证到期日期
- javascript - Vue.js 中的原始表达式:动态计算类中的错误
- java - 通过 RestTemplate 使用 Spring Paged 响应会给出不存在的错误
- tensorflow - InvalidArgumentError:尝试读取字符串时数据太短 [[{{node DecodeWav}}]] [Op:IteratorGetNext]
- database - TDengine 无法删除节点,总是显示正在删除
- c - 当本地构建成功时,调试 Azure Pipeline 失败构建的正确方法是什么?
- android - BMI Calculator in Kotlin