简体   繁体   English

注意:试图获取非对象的属性

[英]Notice: Trying to get property of non-object

I am getting this error: 我收到此错误:

( ! ) Notice: Trying to get property of non-object in C:\\wamp\\www\\admin\\paginator\\Paginator.class.php on line 18. (!)注意:试图在第18行的C:\\ wamp \\ www \\ admin \\ paginator \\ Paginator.class.php中获取非对象的属性。

index page: 索引页:

<?php 
 require_once 'paginator/Paginator.class.php';

    $conn       = new mysqli( 'localhost', 'USER', 'PASS' );
     mysqli_select_db($link, "DB");
    $limit      = ( isset( $_GET['limit'] ) ) ? $_GET['limit'] : 25;
    $page       = ( isset( $_GET['page'] ) ) ? $_GET['page'] : 1;
    $links      = ( isset( $_GET['links'] ) ) ? $_GET['links'] : 7;


    $query      = "
SELECT utilizatori.id, utilizatori.utilizator, utilizatori.nume, utilizatori.rol_user 
AS ID, LOGIN, NUME, ROL 
FROM utilizatori
ORDER BY `utilizator` ASC";

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

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

paginator.class.php: paginator.class.php:

<?php

class Paginator {

        private $_conn;
        private $_limit;
        private $_page;
        private $_query;
        private $_total;


public function __construct( $conn, $query ) {

    $this->_conn = $conn;
    $this->_query = $query;

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

Where the 18 line is: 第18行是:

$this->_total = $rs->num_rows;

I checked everything but can't figure out where is the problem. 我检查了所有内容,但不知道问题出在哪里。 Can anyone see where is the problem more than me, please? 请问谁能比我更多地了解问题所在?

The quick answer is: $rs is not an object. 快速的答案是: $rs不是对象。 And therefore it has neither properties nor methods, and you cannot treat it like an object like so: $rs->num_rows . 因此,它既没有属性也没有方法,并且不能将其像这样的对象对待: $rs->num_rows

I assume (since num_rows is a property of mysqli_result ) that your class's property $this->_conn is a mysqli object. 我假设(由于num_rowsmysqli_result的属性),您的类的属性$this->_conn mysqli_result $this->_connmysqli对象。 If you look at the documentation for mysqli::query() , you'll see that this method will return: 如果您查看mysqli::query()文档 ,则会看到此方法将返回:

  • mysqli_result object for SELECT , SHOW , DESCRIBE or EXPLAIN SELECTSHOWDESCRIBEEXPLAIN mysqli_result对象
  • false for failure 因失败而false
  • true for other successful queries true于其他成功查询

In short, $rs is not a mysqli_result in your example. 简而言之, $rs在您的示例中不是mysqli_result Your query is either not one of the above listed, or it is failing. 您的查询不是以上列出的查询之一,否则失败。

Perhaps you could make your code more robust with something like: 也许您可以通过以下方式使代码更健壮:

if (false === $rs) {
    // uh oh...
    throw new RuntimeException(
        sprintf('mysqli error! %s', $this->_conn->connect_error)
    );   
}

Please note that is not tested. 请注意,未经测试。 Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM