Sto cercando di scrivere una class personalizzata per MySQLi, ma continuo a ricevere l'errore "Cercando di get la properties; di non object in" quando si utilizzano num_rows
. Qualcuno può aiutare?
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 () non restituisce sempre un object mysqli_result , può anche restituire TRUE
e FALSE
e ciò causerebbe l'errore che si sta ottenendo.
Memorizza il valore restituito di query()
e verifica se è TRUE
o FALSE
prima di provare ad accedervi come object e recuperare la properties; num_rows
.
Dal manuale su mysqli::query()
:
Restituisce FALSE in caso di fallimento. Per il successo delle query SELECT , SHOW , DESCRIBE o EXPLAIN
mysqli_query()
restituirà un objectmysqli_result
. Per altre query di successomysqli_query()
restituirà TRUE .