I've just spent the last 4 hours tracing a bug in my script. I kept receiving the following error:
Call to a member function fetch_assoc on a non object
This was being thrown when I did something like:
$query = $database -> query("SELECT .....")
while($q = $query) {...}
My database -> query
method looks like: (where $database -> mysqli
is the mysqli
object)
function query($stmt) {
$query = $this -> mysqli -> query($stmt);
$id = $this -> mysqli -> insert_id;
return $id === 0 ? $query : $id;
}
Now, this returns the inserted autoincrement id for an insert row, otherwise the result of the query. I just found out that if I run an insert query and then a select query, the second query will return the insert id of the insert query run beforehand. Is this really the intended behaviour, or am I missing something here?