从数据库查询数据时在php中获取错误

xwmevbvl  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(356)

这个问题在这里已经有答案了

mysql\u fetch\u array()/mysql\u fetch\u assoc()/mysql\u fetch\u row()/mysql\u num\u rows等…期望参数1是资源(31个答案)
mysqli_fetch_assoc()需要参数/调用成员函数bind_param()时出错。如何获得实际的mysql错误并修复它(1个答案)
两年前关门了。
这是我的php代码

<?php

if (isset($_POST['search'])) {
    require 'config.php';
    $department = $_POST['department'];
    $class = $_POST['class'];
    $i = 1;
    $query = "SELECT `student_id` , `name`, `htno`, `department`, `class` FROM student_detail where department=$department && class=&class";
    $result = mysqli_query($conn, $query);
    while ($row = mysqli_fetch_array($result)) {
        echo "<tr>";
        echo "<td>" . $i++ . "</td>";
        echo "<td>" . $row['name'] . "</td>";
        echo "<td>" . $row['htno'] . "</td>";
        echo "<td>" . $row['department'] . "</td>";
        echo "<td>" . $row['class'] . "</td>";
        echo "<td><a href=\"delete.php?class_id=" . $row['student_id'] . "\" onClick=\"return confirm('Are you sure you want to delete?')\">DELETE</a></td></tr>";
    }
}
?>

错误是
警告:mysqli\u fetch\u array()要求参数1为mysqli\u result,布尔值`
当我使用这个代码是我做错了什么请帮助我。。我是php初学者。我无法选择数据库。
选择数据时,错误可能在部门和类中。

<form method="post" action="" >
    <div class="form-group">
        <label for="department">Department</label>
        <select name="department" class="form-control">
            <option selected="selected">Please select your department</option>
            <?php
            require('config.php');
            $result = mysqli_query($conn, "SELECT * FROM department");

            while ($test = mysqli_fetch_array($result)) {
                echo "<option 
              value='" . $test['department_name'] . "'>" . $test['department_name'] . "</option>";
            }
            ?>
        </select>
    </div>
    <div class="form-group ">
        <label for="class">Class</label>
        <select name="class" class="form-control">
            <option selected="selected">Please select your class</option>
            <?php
            require('config.php');
            $result = mysqli_query($conn, "SELECT * FROM class");

            while ($test = mysqli_fetch_array($result)) {
                echo "<option value='" . $test['class_name'] . "'>" . $test['class_name'] . "
    </option>";
            }
            ?>
        </select>
    </div>
    <button type="submit" name="search" class="btn btn-
            primary">Submit</button>
</form>
uxhixvfz

uxhixvfz1#

试试这个方法。这将在查询中使用准备好的语句。

$query = "SELECT 
            `student_id`,
            `name`,
            `htno`,
            `department`,
            `class`
            FROM student_detail 
            where department = ? && class = ? ";

            $stmt = $conn->prepare($query);
            $stmt->bind_param('ss', $department, $class);  // s - string, i - ints
            $stmt->execute();
            $result = $stmt->get_result();  

            while ($row = $result->fetch_assoc()) {
            //...Do the rest of code here.

以下是有关mysqli准备好的语句的链接:
https://websitebeaver.com/prepared-statements-in-php-mysqli-to-prevent-sql-injection

相关问题