【问题标题】:where to put limit in mysqli query in pagination在分页中将mysqli查询限制在哪里
【发布时间】:2018-01-06 17:15:25
【问题描述】:

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

 $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']."'," ); 

【问题讨论】:

  • 两个原因:where 子句后面有一个逗号,并且 LIMIT 必须在查询的末尾。
  • MySQL 文档包含每种查询类型的语法图。既然可以访问网站,为什么还要在这里发帖?

标签: php mysqli pagination


【解决方案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);

【讨论】:

  • 为什么不用$start,$limit的参数?
猜你喜欢
  • 1970-01-01
  • 2021-05-15
  • 2015-03-13
  • 1970-01-01
  • 1970-01-01
  • 2020-12-19
  • 2011-08-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多