2017-04-03 50 views
-5

我以php的形式创建了搜索过滤功能。从表中获取来自mysql的数据。所有查询和其他人写得很好。但搜索功能不起作用。PHP搜索过滤功能不起作用

<?php 
    require_once 'init.php'; 
    include 'header.php'; 
    include 'navigation.php'; 

    if(isset($_POST['search'])){ 
     $valSearch = $_POST['searchButton']; 
     $query = "SELECT * FROM mainTable WHERE CONCAT ('id','commandName','commandDesc','status') LIKE '%".$valSearch."%'"; 
     $featured = filterTable($query); 
    }else{ 
     $query = "SELECT * FROM mainTable"; 
     $featured = filterTable($query); 
    } 

    function filterTable($query){ 
     $connect = mysqli_connect("localhost","root","","linuxcommanddictionary"); 
     $filter_result = mysqli_query($connect,$query); 
     return $filter_result; 
    } 

?> 
<form action = "index.php" method="post"> 
    <div class="container"> 
     <h3>Linux Fedora OS</h3><hr> 
     <button type="submit" name ="searchButton" class="btn btn-default"><span class="glyphicon glyphicon-search"></span></button> 
     <input type="text" name = "search" class="form-control" placeholder="Хайх үгээ бичнэ үү"> 
     <div class="col-md-12"> 
      <div class="row"> 
       <table class="table table-bordered"> 
        <thead> 
         <th>ID</th> 
         <th>Command Name</th> 
         <th>Command Description</th> 
         <th>Status</th> 
        </thead> 
        <tbody> 
         <?php while($result = mysqli_fetch_assoc($featured)): ?> 
         <tr> 
          <td><?=$result['id']; ?></td> 
          <td><?=$result['commandName']; ?></td> 
          <td><?=$result['commandDesc']; ?></td> 
          <td><?=$result['status']; ?></td> 
         </tr> 
         <?php endwhile; ?> 
        </tbody> 
       </table> 
      </div> 
     </div> 
    </div> 
</form> 
<?php 
    include 'footer.php'; 
?> 
</body> 
</html> 

这是数据库结构。

create table if not exists mainTable(
    id int auto_increment, 
    commandName varchar(255), 
    commandDesc varchar(255), 
    status int(2), 
    primary key(id) 
) 
+0

它不工作是不是从开发者的一个很好的解释

function filterTable($query){ global $connect; $connect = mysqli_connect("localhost","root","","linuxcommanddictionary"); $filter_result = mysqli_query($connect,$query); // fetch results here as mysqli_query returns true/false. $final_results = array(); while($row=mysqli_fetch_array($filter_result,MYSQLI_NUM)){ $final_results[] = $row; } return $final_results; } 

3-转。你会得到什么错误? – Akintunde007

+1

也似乎你需要通过你的连接变量作为你的过滤器表函数的全局变量 – Akintunde007

+0

没有出现任何错误。 – ARLEQUINA

回答

0

,因为你说你没有任何错误,请尝试以下操作:

1-通过您的phpmyadmin手动运行查询,看看你得到任何结果。

2-将您的连接变量作为全局变量进行PAss。了解更多关于变量scopes here更好地理解这些概念所涉及的使用error_reporting,看是否发生了任何事情

0

正如在评论中提到,要调用函数内部的mysqli连接,然后索要mysqli_fetch_assoc()的函数的“范围”之外。

  1. 学习范围http://php.net/manual/en/language.variables.scope.php

您可以 -move while循环的函数中,并返回一个数组的“复制”(记忆吸了)。 - 或 - 在全局范围内创建连接器,并在函数中调用'global'来获取数据(cpu吸取) - 或者 - 将所有内容全部移到全局范围内。

+0

你的问题缺乏很多,我甚至试图编辑它,你没有足够的'内容'甚至被编辑。请做好你的功课,这样我们可以更好地帮助你。 https://stackoverflow.com/help/how-to-ask。 –

+1

谢谢!理解:) – ARLEQUINA