我收到此错误:
(!)注意:试图在第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;
我检查了所有内容,但不知道问题出在哪里。请问谁能比我更多地了解问题所在?
快速的答案是:$rs
不是对象。因此,它既没有属性也没有方法,并且您不能将其像这样的对象对待:$rs->num_rows
。
我假设(由于num_rows
是的一个属性mysqli_result
)您的类的属性$this->_conn
是一个mysqli
对象。如果查看的文档mysqli::query()
,您将看到此方法将返回:
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)
);
}
请注意,未经测试。希望这可以帮助 :)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句