0

Whenever a query finishes executing, I know you should free the result. Here is a snippet of a class I built for running a simple query. Could someone tell me where I went wrong? The query, when entered properly, runs successfully. It's just that my page doesn't reload like it, should but it gives me these errors...

Fatal error: Call to a member function free() on a non-object in <file> on <line>

... and this error ...

Fatal error: Call to a member function close() on a non-object in <file> on <line>

Here a simplified version of my code:

public function query($query) {
  try {
    if ($result = $this->connection->query($query, MYSQLI_USE_RESULT)) {
      $result->free();  //Problems here
      $result->close(); //Problems here
                
      return $result;
    } else {
      $result->free();  //Problems here
      $result->close(); //Problems here
      
      throw new Exception("The query was invalid");
    }
  } catch (Exception $e) {
    die($e->getMessage());
  }
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195
  • Does this answer your question? [mysqli\_fetch\_assoc() expects parameter / Call to a member function bind\_param() errors. How to get the actual mysql error and fix it?](https://stackoverflow.com/questions/22662488/mysqli-fetch-assoc-expects-parameter-call-to-a-member-function-bind-param) – Dharman Jun 24 '20 at 00:05

1 Answers1

5

If I follow your link to the manual, there is something mentioned about the return value

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a result object. For other successful queries mysqli_query() will return TRUE.

Especially you don't handle the two cases true and more important false

if ($result = $this->connection->query($query, MYSQLI_USE_RESULT)) {
  $result->free();  // Probably call "free()" on true
  $result->close();

  return $result;
} else {  // <-- false, null, 0 or ""
  $result->free();  // Probably call "free()" on false
  $result->close();

  throw new Exception("The query was invalid");
}

I don't know, what $query is in your case, but query() will only return a result, if there is one (SELECT-query).

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • 1
    Of course. As I suggest only `SELECT`-queries has results. Everything else only return "success" (`true`) or "failure" (`false`). Even worse you test, if the result evaluates to `false` (the `else`-branch), but you call `free()` on it anyway. – KingCrunch Jun 16 '11 at 19:55
  • After looking at your description again, yeah, that looks like you hit the nail on the head. That being said, is there a way I can see if this is an object? – Oliver Spryn Jun 16 '11 at 19:55
  • 3
    `is_object()`, or just test against the booleans `=== true`, or `=== false` – KingCrunch Jun 16 '11 at 19:57
  • I'm asking too many questions. :( `is_object()` will tell me if I have an object. Thanks for your help, KingCrunch! – Oliver Spryn Jun 16 '11 at 19:58