注意:试图获取非对象的 属性
Notice: Trying to get property of non-object
我收到此错误:
( ! ) Notice: Trying to get property of non-object in C:\wamp\www\admin\paginator\Paginator.class.php on line 18.
索引页:
<?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;
我检查了所有内容,但无法找出问题出在哪里。有谁比我更能看出问题出在哪里吗?
快速回答是:$rs
不是对象。因此它既没有属性也没有方法,你不能像这样对待它:$rs->num_rows
.
我假设(因为 num_rows
是 mysqli_result
的 属性)你的 class 的 属性 $this->_conn
是 mysqli
对象。如果您查看 documentation for mysqli::query()
,您会发现此方法将 return:
mysqli_result
SELECT
、SHOW
、DESCRIBE
或 EXPLAIN
对象
false
失败
true
其他成功查询
简而言之,$rs
不是您示例中的 mysqli_result
。您的查询不是上面列出的查询之一,或者查询失败。
也许您可以通过以下方式使您的代码更健壮:
if (false === $rs) {
// uh oh...
throw new RuntimeException(
sprintf('mysqli error! %s', $this->_conn->connect_error)
);
}
请注意,未经测试。希望这有帮助:)
我收到此错误:
( ! ) Notice: Trying to get property of non-object in C:\wamp\www\admin\paginator\Paginator.class.php on line 18.
索引页:
<?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;
我检查了所有内容,但无法找出问题出在哪里。有谁比我更能看出问题出在哪里吗?
快速回答是:$rs
不是对象。因此它既没有属性也没有方法,你不能像这样对待它:$rs->num_rows
.
我假设(因为 num_rows
是 mysqli_result
的 属性)你的 class 的 属性 $this->_conn
是 mysqli
对象。如果您查看 documentation for mysqli::query()
,您会发现此方法将 return:
mysqli_result
SELECT
、SHOW
、DESCRIBE
或EXPLAIN
对象
false
失败true
其他成功查询
简而言之,$rs
不是您示例中的 mysqli_result
。您的查询不是上面列出的查询之一,或者查询失败。
也许您可以通过以下方式使您的代码更健壮:
if (false === $rs) {
// uh oh...
throw new RuntimeException(
sprintf('mysqli error! %s', $this->_conn->connect_error)
);
}
请注意,未经测试。希望这有帮助:)