1

我收到此错误:

( ! ) 注意:试图在第 18 行的 C:\wamp\www\admin\paginator\Paginator.class.php 中获取非对象的属性。

索引页:

<?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:

<?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;

第 18 行是:

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

我检查了所有内容,但无法弄清楚问题出在哪里。谁能比我看到问题出在哪里,好吗?

4

1 回答 1

2

快速的回答是:$rs不是一个对象。因此它既没有属性也没有方法,你不能像这样对待它:$rs->num_rows.

我假设(因为num_rows是 的属性mysqli_result)你的类的属性$this->_conn是一个mysqli对象。如果您查看 的文档mysqli::query(),您会看到此方法将返回:

  • mysqli_resultSELECT, SHOW,DESCRIBE或的对象EXPLAIN
  • false对于失败
  • true对于其他成功的查询

总之,$rs不是mysqli_result你的例子。您的查询不是上面列出的之一,或者它失败了。

也许您可以通过以下方式使您的代码更加健壮:

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

请注意,未经测试。希望这可以帮助 :)

于 2015-08-16T01:09:15.523 回答