在分页中将mysqli查询限制放在何处

时间:2018-01-05 22:37:48

标签: php mysqli pagination

所以我的查询就在这里我现在正在使用php进行分页现在每当我在我的查询中设置限制就会出现此错误警告:mysqli_fetch_array()期望参数1为mysqli_result,在C:\ xampp \ htdocs中给出布尔值第292行\ project \ Deshboard.php

 $query=mysqli_query($conn,"SELECT *

 FROM 
 notes LIMIT $start,$limit

INNER JOIN 
 subjects

 ON

subjects.subject_id = notes.subject_id
WHERE   by_teacher='".$_SESSION['admin_id']."'," ); 

1 个答案:

答案 0 :(得分:1)

首先,欢迎来到Stack Overflow!

我现在多次阅读查询以为您创建一个可靠的答案,并且必须首先在接受参数时忘记prepared queries的重要性。这样可以避免对您的数据库进行SQL injection /恶意攻击。

完成此操作后,根据SQL SELECT-syntaxLIMIT应位于WHERE声明之后。

PHP代码应该看起来像这样:

    // prepare a mysqli query with notes.by_teacher requiring
    // a parameter (indicated by the question mark)
    $stmt = mysqli_prepare(
        $conn, 'SELECT * FROM notes 
            INNER JOIN subjects ON subjects.subject_id = notes.note_id
            WHERE notes.by_teacher=?
            LIMIT ?,?'
    );

    // set the first parameter as type string (s)
    // to the value of $_SESSION['admin_id']
    mysqli_stmt_bind_param($stmt, 'sii', $_SESSION['admin_id'], $start, $limit);