【问题标题】:Cant filter from database无法从数据库中过滤
【发布时间】:2019-01-26 13:11:50
【问题描述】:

我遇到了这个问题,当我搜索一个单词时,它不会显示并且会弹出这个错误

警告:mysqli_fetch_array() 期望参数 1 为 mysqli_result,布尔值在 C:\xampp\htdocs\public_html\filterdata.php 第 213 行给出)。

只显示过滤前的表格

<?php
include("auth_admin.php");


if(isset($_POST['search']))
{
$valueToSearch = $_POST['valueToSearch'];
// search in all table columns
// using concat mysql function
$query = "SELECT * FROM `lcho_dengue_activities` CONCAT(`id`, `month`, `year`, `dengue_ind1`) where `month`= '".$valueToSearch."'";
$search_result = filterTable($query);


}
else {
$query = "SELECT * FROM `lcho_dengue_activities`";
$search_result = filterTable($query);

 }

// function to connect and execute the query
function filterTable($query)
 {
$connect = mysqli_connect("localhost", "root", "", "lcho_login");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
 }


 ?>

<form action="filterdata.php" method="post">
        <input type="text" name="valueToSearch" placeholder="Value To Search"><br><br>
        <input type="submit" name="search" value="Filter"><br><br>

        <table>
            <tr>
                <th>Id</th>
                <th>Month</th>
                <th>Year</th>
                <th>dengue_ind1</th>
            </tr>

  <!-- populate table from mysql database -->
         <?php while($row = mysqli_fetch_array($search_result)):?>
            <tr>
                <td><?php echo $row['id'];?></td>
                <td><?php echo $row['month'];?></td>
                <td><?php echo $row['year'];?></td>
                <td><?php echo $row['dengue_ind1'];?></td>

            </tr>
            <?php endwhile;?>
        </table>
    </form>

//while($row = mysqli_fetch_array($search_result)) 是我的第 213 行

我尝试将 $search_result 更改为 $query 并发生同样的错误。

【问题讨论】:

  • 警告:您对SQL Injections 持开放态度,应该真正使用参数化的prepared statements,而不是手动构建查询。它们由PDOMySQLi 提供。永远不要相信任何类型的输入,尤其是来自客户端的输入。即使您的查询仅由受信任的用户执行,you are still in risk of corrupting your data

标签: php filter


【解决方案1】:

该查询失败并返回 false

把这个放在mysqli_query()之后看看发生了什么。

if (!$filter_Result) {
   printf("Error: %s\n", mysqli_error($connect));
   exit();
}

更多信息:

http://www.php.net/manual/en/mysqli.error.php

【讨论】:

  • 错误:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 '(id, month, year, dengue_ind1) where month= 'january'' 附近使用正确的语法
猜你喜欢
  • 2018-07-08
  • 1970-01-01
  • 2019-03-18
  • 1970-01-01
  • 2018-08-08
  • 2019-05-31
  • 2010-12-06
  • 1970-01-01
相关资源
最近更新 更多