非对象上的Fetch_Assoc()

时间:2015-01-16 01:18:06

标签: php mysqli

所以,我收到了这个错误:

Fatal error: Call to a member function fetch_assoc() on a non-object

它说错误在第220行。

第220行是:

while ($field = $result->fetch_assoc()) {

$结果:

$tbl_name = "savage";
$sql = "SELECT * FROM $tbl_name LIMIT $start, $limit ORDER BY `id` DESC";
$result = $mysqli->query($sql);

我不确定是不是因为$ tbl_name在变量中或者是什么,但我还没有想到如何解决这个问题。可能是一个简单的答案,但希望你能帮忙!如果我需要提供更多信息,请告诉我们。

忘了包括:

  $limit = 10;
  if(isset($_GET['page'])) {
    $page = $_GET['page'];
    $start = ($page - 1) * $limit;
  } else {
    $page = 0;
    $start = 0;
  }

在这种情况下,$ _GET [' page']未设置。

1 个答案:

答案 0 :(得分:1)

LIMIT追踪ORDER BY

$sql = "SELECT * FROM $tbl_name ORDER BY `id` DESC LIMIT $start, $limit";

手册中的示例:

SELECT
    [ALL | DISTINCT | DISTINCTROW ]
      [HIGH_PRIORITY]
      [STRAIGHT_JOIN]
      [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
      [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
    select_expr [, select_expr ...]
    [FROM table_references
    [WHERE where_condition]
    [GROUP BY {col_name | expr | position}
      [ASC | DESC], ... [WITH ROLLUP]]
    [HAVING where_condition]
    [ORDER BY {col_name | expr | position}
      [ASC | DESC], ...]
    [LIMIT {[offset,] row_count | row_count OFFSET offset}]