I'm trying to write a custom class for MySQLi, but I keep receiving the error "Trying to get property of non-object in" when using num_rows. Can anyone help?
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()returns amysql_resultresource. Your query$sqlfails for some reason and you perform no error check before attempting to getmysql_result::$num_rowsfrom it.