我有一个包含100个左右用户的用户列表,我尝试创建搜索字段,以便更轻松地找到某个用户。我想在用户数据库中搜索多个列。
我尝试创建此查询,但我对此很新,我不确定所有的OR语句。
这是我得到的。
我的搜索表单
<form action="search-users.php" method="GET" class="no-margin">
<label>Type in a keyword to search for a user.</label><br><br>
<input type="text" name="keywords" autocomplete="off" placeholder="Example: John Doe"><br>
<input type="submit" value="Search Users">
</form>
然后是动作脚本:
<?php
if(isset($_GET['keywords'])){
$keywords = escape($_GET['keywords']);
$search = DB::getInstance()->query("
SELECT `username`,`first_name`,`last_name`,`unit`,`email`,`rent_own`,`city`,`zip`,`phone` FROM `users` WHERE
`username` LIKE '%{keywords}%' OR
`first_name` LIKE '%{keywords}%' OR
`last_name` LIKE '%{keywords}%' OR
`unit` LIKE '%{keywords}%' OR
`email` LIKE '%{keywords}%' OR
`rent_own` LIKE '%{keywords}%' OR
`city` LIKE '%{keywords}%' OR
`zip` LIKE '%{keywords}%' OR
`phone` LIKE '%{keywords}%'
");
}
?>
<!--SHOW HOW MANY RESULT ARE RETURNED-->
<h3 class="blue-tx" align="center">Found <?php echo $search->Count()?> results</h3>
<?php
if ($search->Count()){
foreach($search->results() as $s){
?>
....Display user results here.
<?php } } ?>
如果有人可以指导我这一点。我很感激。
答案 0 :(得分:1)
您在关键字变量
中忘记了$
变化:
&#39;%{关键字}%&#39; =&GT; &#39;%{$关键字}%&#39;
答案 1 :(得分:0)
试试这个:
<?php
if(isset($_GET['keywords'])){
$keywords = escape($_GET['keywords']);
$search = DB::getInstance()->query("
SELECT `username`,`first_name`,`last_name`,`unit`,`email`,`rent_own`,`city`,`zip`,`phone` FROM `users` WHERE
`username` LIKE '%keywords%' OR
`first_name` LIKE '%keywords%' OR
`last_name` LIKE '%keywords%' OR
`unit` LIKE '%keywords%' OR
`email` LIKE '%keywords%' OR
`rent_own` LIKE '%keywords%' OR
`city` LIKE '%keywords%' OR
`zip` LIKE '%keywords%' OR
`phone` LIKE '%keywords%'
");
}
?>
<!--SHOW HOW MANY RESULT ARE RETURNED-->
<h3 class="blue-tx" align="center">Found <?php echo $search->Count()?> results</h3>
<?php
if ($search->Count()){
foreach($search->results() as $s){
?>
....Display user results here.
<?php } } ?>