簡體   English   中英

發生致命錯誤:在第32行的C:\\ wamp \\ www \\ quotes \\ paginator.php中的非對象上調用成員函數query()

[英]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()

我應該怎么做才能擺脫這個錯誤? 我必須瀏覽以下頁面。 我閱讀了給我的參考,並從代碼中尋求幫助,但是我遇到了致命錯誤。 如上。請看一下我的代碼,告訴我落后了。 我有兩頁。 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>

paginator.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->_con = $con;

但將其稱為$this->_conn->

那么你是構造函數的不正確

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

編輯:並且應該是:(我認為很明顯應該添加它)

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

您缺少該構造的下划線

在開發階段,請始終檢查錯誤。

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


錯誤報告添加到文件頂部,這將有助於發現錯誤。

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

// rest of your code

旁注:顯示錯誤僅應在登台中進行,而不應在生產中進行。

問題似乎出在線路上:

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

您的類Paginator具有名為_con而不是_conn的經典復制粘貼錯誤變量。

編輯:

在OP提到他在此處粘貼的代碼不正確之后,似乎_con對象為null,但_con其設置為構造函數。 但是,從閉包的角度看,根本沒有構造函數。

此處定義了構造函數magic方法的語法

在Paginator.php中將$this->_con this- $this->_conn$this->_con this- $this->_con

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM