【问题标题】:Getting Fatal error: Call to a member function query() on a non-object in C:\wamp\www\quotes\paginator.php on line 32出现致命错误:在第 32 行的 C:\wamp\www\quotes\paginator.php 中的非对象上调用成员函数 query()
【发布时间】:2015-07-31 20:07:14
【问题描述】:

我正在尝试进行分页,但我得到了

致命错误:在 C:\wamp\www\quotes\paginator.php 第 32 行对非对象调用成员函数 query()

我应该怎么做才能摆脱这个错误?我必须页面如下。 我阅读了给我的参考资料并从代码中获得了帮助,但我遇到了致命错误。如上所述。请查看我的代码并告诉我落后的地方。 我有两页。 index.php 和 paginator.php 为什么我会遇到我无法理解的致命错误。

index.php

     <!DOCTYPE html>

    <?php
    require_once 'Paginator.php';

    $con       = new mysqli( 'localhost', 'root', 'pixster', 'quotes' );
    $limit      = ( isset( $_GET['limit'] ) ) ? $_GET['limit'] : 25;
    $page       = ( isset( $_GET['page'] ) ) ? $_GET['page'] : 1;
    $links      = ( isset( $_GET['links'] ) ) ? $_GET['links'] : 7;
    $query      = "SELECT table2.col2 AS a,table1.col2 AS b, table1.col1 AS  c, table1.q_url AS d FROM table2, table1 WHERE table2.col1 = table1.col4 ";//  AND table2.friendly_url= '".$authorname."'";

   $Paginator  = new Paginator( $con, $query );

   $results    = $Paginator->getData( $page, $limit );
   ?>
   <?php for( $i = 0; $i < count( $results->data ); $i++ ) : ?>
   <tr>
   <td><?php echo $results->data[$i]['$i']; ?></td>
   <td><?php echo $results->data[$i]['$row['b']']; ?></td>

    </tr>
    <?php endfor; ?>
    <html>
    <head>
    <title>quotes Pagination</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    </head>
    <body>
    <div class="container">
    <div class="col-md-10 col-md-offset-1">
    <h1>PHP Pagination</h1>
    <table class="table table-striped table-condensed table-bordered table- rounded">
    <thead>
    <tr>
    <th width="20%">Sr. No</th>
    <th width="20%">Quotes</th>

      </tr>
      </thead>
      <tbody>
      <?php for( $i = 0; $i < count( $results->data ); $i++ ) : ?>
      <tr>
      <td><?php echo $results->data[$i]['$i']; ?></td>
      <td><?php echo $results->data[$i]['$row['b']']; ?></td>

      </tr>
      <?php endfor; ?></tbody>
      </table>
      <?php echo $Paginator->createLinks( $links, 'pagination pagination-sm' ); ?> 
    </div>
    </div>
    </body>
    </html>

分页器.php

      <?php

     class Paginator {

    private $_con;
    private $_limit;
    private $_page;
    private $_query;
    private $_total;


  public function _construct( $con, $query ) 
 {

 $this->_con = $con;
 $this->_query = $query;

 $rs= $this->_con->query( $this->_query );
 $this->_total = $rs->num_rows;

 }

 public function getData( $limit = 10, $page = 1 ) {

$this->_limit   = $limit;
$this->_page    = $page;

 if ( $this->_limit == 'all' ) {
    $query      = $this->_query;
 } else {
    $query      = $this->_query . " LIMIT " . ( ( $this->_page - 1 ) *     $this->_limit ) . ", $this->_limit";
  }
  $rs             = $this->_conn->query( $query );

    while ( $row = $rs->fetch_assoc() ) {
    $results[]  = $row;
    }

    $result         = new stdClass();
    $result->page   = $this->_page;
    $result->limit  = $this->_limit;
    $result->total  = $this->_total;
    $result->data   = $results;

    return $result;
    }  

  public function createLinks( $links, $list_class ) {
   if ( $this->_limit == 'all' ) {
    return '';
    }

   $last       = ceil( $this->_total / $this->_limit );

   $start      = ( ( $this->_page - $links ) > 0 ) ? $this->_page - $links :   1;
  $end        = ( ( $this->_page + $links ) < $last ) ? $this->_page +   $links : $last;

  $html       = '<ul class="' . $list_class . '">';

    $class      = ( $this->_page == 1 ) ? "disabled" : "";
    $html       .= '<li class="' . $class . '"><a href="?limit=' . $this->_limit . '&page=' . ( $this->_page - 1 ) . '">&laquo;</a></li>';

    if ( $start > 1 ) {
    $html   .= '<li><a href="?limit=' . $this->_limit . '&page=1">1</a>  </li>';
     $html   .= '<li class="disabled"><span>...</span></li>';
     }

    for ( $i = $start ; $i <= $end; $i++ ) {
    $class  = ( $this->_page == $i ) ? "active" : "";
    $html   .= '<li class="' . $class . '"><a href="?limit=' . $this->_limit  .     '&page=' . $i . '">' . $i . '</a></li>';
  }

    if ( $end < $last ) {
    $html   .= '<li class="disabled"><span>...</span></li>';
    $html   .= '<li><a href="?limit=' . $this->_limit . '&page=' . $last .  '">' . $last . '</a></li>';
   }

 $class      = ( $this->_page == $last ) ? "disabled" : "";
 $html       .= '<li class="' . $class . '"><a href="?limit=' . $this- >_limit . '&page=' . ( $this->_page + 1 ) . '">&raquo;</a></li>';

  $html       .= '</ul>';

  return $html;
  }
  }
  ?>                

【问题讨论】:

  • 咳咳$this-&gt;_conn-&gt;
  • 我应该在哪里改变兄弟??
  • 您已将其声明为$this-&gt;_con = $con;
  • 您在执行查询时似乎从不检查连接问题或错误。
  • 现在就开始回答吧。我已经说了要让你开始的话。

标签: php mysql pagination


【解决方案1】:

首先,您将连接声明为

$this->_con = $con;

但称它为$this-&gt;_conn-&gt;

那你就是构造函数不正确

public function _construct( $con, $query )
               ^ missing one here

edit: 应该是:(我觉得很明显应该加上)

public function __construct( $con, $query )
                ^^ there are 2 now

结构缺少下划线

始终在开发阶段检查错误。

参考:http://php.net/manual/en/language.oop5.php


error reporting 添加到文件顶部,这将有助于查找错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

旁注:只应在暂存阶段显示错误,而不是在生产阶段。

【讨论】:

  • 我在哪里缺少下划线兄弟??我在上面的代码中看不到它。
  • @AlpeshPanchal 两个__
  • @AlpeshPanchal 这里_construct public function _construct( $con, $query )
【解决方案2】:

$this-&gt;_conn 到 Paginator.php 中的$this-&gt;_con

$rs = $this->_con->query( $query );

【讨论】:

  • 我在这里犯了这个错误,不是我的代码,请找到除此之外的答案
  • @AlpeshPanchal 检查 Fred 的答案。我认为他发现了问题__contruct
【解决方案3】:

问题似乎出在这行:

$rs = $this-&gt;_conn-&gt;query( $query );

你的班级 Paginator 有变量名为 _con 而不是 _conn 一个经典的复制粘贴错误。

编辑:

在OP提到他在这里粘贴的代码不正确之后,似乎_con对象为null,但它被设置为构造函数。然而,从闭包的角度来看,根本就没有构造函数。

构造函数魔术方法的语法定义为here

【讨论】:

  • 我在这里犯了这个错误,不是我的代码,请找到除此之外的答案
猜你喜欢
  • 1970-01-01
  • 2015-02-15
  • 1970-01-01
  • 2014-10-21
  • 1970-01-01
  • 2014-02-10
  • 2015-04-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多