php – Fatal error: Call to a member function bind_param() on boolean

php – Fatal error: Call to a member function bind_param() on boolean

The problem lies in:

$query = $this->db->conn->prepare(SELECT value, param FROM ws_settings WHERE name = ?);
$query->bind_param(s, $setting);

The prepare() method can return false and you should check for that. As for why it returns false, perhaps the table name or column names (in SELECT or WHERE clause) are not correct?

Also, consider use of something like $this->db->conn->error_list to examine errors that occurred parsing the SQL. (Ill occasionally echo the actual SQL statement strings and paste into phpMyAdmin to test, too, but theres definitely something failing there.)

Any time you get the…

Fatal error: Call to a member function bind_param() on boolean

…it is likely because there is an issue with your query. The prepare() might return FALSE (a Boolean), but this generic failure message doesnt leave you much in the way of clues. How do you find out what is wrong with your query? You ask!

First of all, make sure error reporting is turned on and visible: add these two lines to the top of your file(s) right after your opening <?php tag:

error_reporting(E_ALL);
ini_set(display_errors, 1);

If your error reporting has been set in the php.ini you wont have to worry about this. Just make sure you handle errors gracefully and never reveal the true cause of any issues to your users. Revealing the true cause to the public can be a gold engraved invitation for those wanting to harm your sites and servers. If you do not want to send errors to the browser you can always monitor your web server error logs. Log locations will vary from server to server e.g., on Ubuntu the error log is typically located at /var/log/apache2/error.log. If youre examining error logs in a Linux environment you can use tail -f /path/to/log in a console window to see errors as they occur in real-time….or as you make them.

Once youre squared away on standard error reporting adding error checking on your database connection and queries will give you much more detail about the problems going on. Have a look at this example where the column name is incorrect. First, the code which returns the generic fatal error message:

$sql = SELECT `foo` FROM `weird_words` WHERE `definition` = ?;
$query = $mysqli->prepare($sql)); // assuming $mysqli is the connection
$query->bind_param(s, $definition);
$query->execute();

The error is generic and not very helpful to you in solving what is going on.

With a couple of more lines of code you can get very detailed information which you can use to solve the issue immediately. Check the prepare() statement for truthiness and if it is good you can proceed on to binding and executing.

$sql = SELECT `foo` FROM `weird_words` WHERE `definition` = ?;
if($query = $mysqli->prepare($sql)) { // assuming $mysqli is the connection
    $query->bind_param(s, $definition);
    $query->execute();
    // any additional code you need would go here.
} else {
    $error = $mysqli->errno .   . $mysqli->error;
    echo $error; // 1054 Unknown column foo in field list
}

If something is wrong you can spit out an error message which takes you directly to the issue. In this case there is no foo column in the table, solving the problem is trivial.

If you choose, you can include this checking in a function or class and extend it by handling the errors gracefully as mentioned previously.

php – Fatal error: Call to a member function bind_param() on boolean

Even when the query syntax is correct, prepare could return false, if there was a previous statement and it wasnt closed.
Always close your previous statement with

$statement->close();

If the syntax is correct, the following query will run well too.

Leave a Reply

Your email address will not be published.