1

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.

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:

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

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

1 Answers1

2

The quick answer is: $rs is not an object. And therefore it has neither properties nor methods, and you cannot treat it like an object like so: $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. If you look at the documentation for mysqli::query(), you'll see that this method will return:

  • mysqli_result object for SELECT, SHOW, DESCRIBE or EXPLAIN
  • false for failure
  • true for other successful queries

In short, $rs is not a mysqli_result in your example. 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 :)

Darragh Enright
  • 13,676
  • 7
  • 41
  • 48
  • I've answered not knowing you already explained it with a bit more detail. Deleting my answer :) – NemanjaSRB Aug 16 '15 at 01:11
  • 1
    Hi. Well, I am not making that assumption because I don't know what your query is but it's certainly a possibility as it's one of the outcomes. If `$rs` is `false` the query failed, and the `connect_error` property should give you some meaningful information about what might have gone wrong. – Darragh Enright Aug 16 '15 at 01:30
  • 1
    Indeed, the query was the problem. Thank you very much! – Bogdan Constantinescu Aug 19 '15 at 20:56