PHP error on bind_param over 2 SQL Selects

PHP has done it again, another fatal error that took me awhile to debug and it was rightly so. The scenario was this.

  1. select all records from a database and insert into a collection.
  2. select all related records related to a specific relation of the collection.

PHP Logo

Thus by doing so, that means that there will be 2 SQL queries to be done. In Java, I will usually continue with the next sql query after the first is done to combine my results into an object. PHP has that limitations. It looks like it would not allow the same PreparedStatement to be used unless…. it’s closed.

 
//So I did this
$stmt->close();

Then a new $stmt is created.

$stmt = mysqli_prepare($con, $sql);

After running the codes, this error appeared out of nowhere.

Fatal error: Call to a member function bind_param() on a non-object

Some internet searching was done to identify the root cause of it. Somethings that was tried was like freeing the buffer and closing the statement. To no avail, the same result was shown (even when I var_dump() the $stmt it will show false). Lo and behold, after more searching, it looks like there is an option to try out to mitigate and identify this error.

mysqli_report(MYSQLI_REPORT_ALL);

The journey in PHP is just the beginning. Did not know that such things exist. The error was finally identified as a field as an illegal field in the preparedStatement. Problem solved. All we need is just an error reporting like the above.

Leave a Reply