1

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?

Keir Simmons
  • 1,634
  • 7
  • 21
  • 37
  • 1
    last_insert_id() will return the auto_increment value of the last INSERT statement in the same session, whenever this was. I don't know what you expect here with using this function. It doesn't make sense to me. – VMai Aug 09 '14 at 22:29
  • From the docs: ` If the last query wasn't an INSERT or UPDATE statement or if the modified table does not have a column with the AUTO_INCREMENT attribute, this function will return zero.` – Keir Simmons Aug 09 '14 at 22:31
  • MySQL is very clear: [LAST_INSERT_ID](https://dev.mysql.com/doc/refman/5.6/en/information-functions.html#function_last-insert-id) *With no argument, LAST_INSERT_ID() returns a 64-bit value representing the first automatically generated value successfully inserted for an AUTO_INCREMENT column as a result of the most recently executed INSERT statement.* There is even an example with a SELECT in between. What your API does, that may be a different story. – VMai Aug 09 '14 at 22:36
  • [PDO::lastInsertId](http://www.php.net/manual/en/pdo.lastinsertid.php): *Returns the ID of the last inserted row, or the last value from a sequence object, depending on the underlying driver.* [mysqli::insert_id](http://www.php.net/manual/en/mysqli.insert-id.php) *The mysqli_insert_id() function returns the ID generated by a query on a table with a column having the AUTO_INCREMENT attribute. If the last query wasn't an INSERT or UPDATE statement or if the modified table does not have a column with the AUTO_INCREMENT attribute, this function will return zero.* – VMai Aug 09 '14 at 22:39
  • Hmm, I'm confused. I'm quoting the PHP docs here: http://php.net/manual/en/mysqli.insert-id.php – Keir Simmons Aug 09 '14 at 22:39
  • But even with mysqli: for me your code doesn't make sense to me. Please use the [rubber duck debugging method](http://en.wikipedia.org/wiki/Rubber_duck_debugging) :-) – VMai Aug 09 '14 at 22:43
  • My code is supposed to take a query and run it. If the query was an insertion, return the last insert id. If not, return what the query itself returns (asc. array with affected rows etc). From your MySQL description, it makes sense, but from the doc I linked to what is happening doesn't seem to be expected behaviour. – Keir Simmons Aug 09 '14 at 22:48
  • I don't see where this function simplifies things. So I wouldn't use it. But I could be wrong, I could overlooking something obvious too. – VMai Aug 09 '14 at 22:53
  • I've left out some other things its doing, mainly counting queries on the page. I just left in what was relevant to the question. – Keir Simmons Aug 09 '14 at 23:00

0 Answers0