我正在嘗試爲MySQLi編寫自定義類,但在使用num_rows
時,我一直收到錯誤「嘗試獲取非對象的屬性」 。誰能幫忙?「嘗試獲取非對象的屬性」 - 自定義mysqli類
class db {
private $host = "***";
private $user = "***";
private $pass = "***";
private $database;
private $connection;
private $result;
public $sql;
function __construct($database) {
if (!empty($database)) $this->database = $database;
$this->connection = new mysqli($this->host,$this->user,$this->pass,$this->database);
return $this->connection;
}
public function fetchRowNum($sql) {
if (!empty($sql)) {
$this->sql = $sql;
return $this->connection->query($sql)->num_rows;
} else {
throw new Exception("Error fetching row");
}
}
}
'mysqli :: query()'返回一個'mysql_result'資源。您的查詢'$ sql'由於某種原因失敗,您在嘗試從它獲取'mysql_result :: $ num_rows'之前未執行錯誤檢查。 – 2012-07-08 20:57:59