PHP搜索过滤功能不起作用

时间:2017-04-03 07:25:44

标签: php mysql mysqli

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

<?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)
)

2 个答案:

答案 0 :(得分:0)

如评论中所述,您在函数内部调用mysqli连接,然后在&#39; Scope&#39;之外请求mysqli_fetch_assoc()。功能。

  1. 学习范围http://php.net/manual/en/language.variables.scope.php
  2. 你可以 - 在你的功能中移动你的while循环并返回一个&#39; copy&#39;数组(内存吮吸)。 -要么- 在全球范围内创建连接器,并呼叫全球&#39;在你的函数中获取数据(cpu suck up) - 或 - 将所有内容全部移到全局范围内。

答案 1 :(得分:0)

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

1-通过phpmyadmin手动运行查询,看看是否有任何结果。

2-将您的连接变量PAss作为全局变量。阅读变量scopes here 的更多信息,以便更好地理解所涉及的概念

 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;
    }

启用error_reporting以查看是否有任何内容

相关问题