1
Folks I have a question regarding this error:
Notice: Trying to get Property 'num_rows' of non-object in C: xampp htdocs control inc database.php on line 94.
Follows part of the code.
function find( $table = null, $id_tag = null ) {
$database = open_database();
$found = null;
try {
if ($id_tag) {
$sql = "SELECT * FROM " . $table . " WHERE id= " . $id_tag;
$result = $database->query($sql);
if ($result->num_rows > 0) {
$found = $result->fetch_assoc();
}
} else {
$sql = "SELECT * FROM " . $table;
$result = $database->query($sql);
**if ($result->num_rows > 0) {
$found = $result->fetch_all(MYSQLI_ASSOC);**
}
}
} catch (Exception $e) {
$_SESSION['message'] = $e->GetMessage();
$_SESSION['type'] = 'danger'; }
close_database($database);
return $found;
}
that mistake means that
$result
does not contain propertynum_rows
i.e., the query is returning some error attempts follow this example take care to close the result set after the query– 13dev
Basically the query failed because the table or the field does not exist. As far as I see in the parameters can be
null
what makes SQL potentiallyselect * from null where id = null
. Dovar_dump($sql)
before the execution ofquery
to see exactly the command that is running and realize the problem.– Isac