Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

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

ErrorFatal error: Uncaught Error: Call to a member function bind_param() on bool

Short answer

mysqli::prepare() returns false when the statement cannot be prepared, so $stmt is false and the next call to bind_param() fails. The real error is in the prepare step, not bind_param. Turn on mysqli exceptions, read the message, then fix the SQL: a typo, a missing table, a placeholder used for a name, or two statements.

  • Applies to PHP 8.3, PHP 8.4, mysqli, MySQL 8, MariaDB 11
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Make mysqli throw on failure, and wrap the prepare call. The exception message from the server is the real cause.

    <?php
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    try {
        $stmt = $mysqli->prepare("SELECT id FROM users WHERE email = ?");
    } catch (mysqli_sql_exception $e) {
        echo $e->getMessage();
    }
  2. 2Match what you see, then jump to its fix

    1. 1No exception and no warning; $stmt is simply falseError reporting is off, so prepare() fails silentlyFix
    2. 2The exception names an unknown table or column, or a syntax error near a given wordThe SQL itself is invalid for this databaseFix
    3. 3The exception shows a syntax error right at the place where the ? sitsA ? placeholder is used for a table or column nameFix
    4. 4The query contains a semicolon followed by another statement, for example a SET before a SELECTThe string holds more than one statementFix

What does the error look like?

  • Fatal error: Uncaught Error: Call to a member function bind_param() on bool
  • Call to a member function bind_param() on boolean

Cause 1 of 4

Error reporting is off, so prepare() fails silently

1Check

The manual says prepare() returns false on error. Since PHP 8.1 the default report mode throws mysqli_sql_exception, so on 8.1 and later this fatal usually means reporting was switched off in code or config. Older PHP versions defaulted to off.

php -v | head -n 1
grep -rn "mysqli_report\|report_mode" . --include=*.php | head

2Fix

Turn reporting on once, right before you connect. In production, log the exception instead of printing it.

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli($host, $user, $pass, $db);

3Verify

A bad query now throws mysqli_sql_exception with the server's message.

php your-script.php

Confirmed by php.net

Did this fix it?

Cause 2 of 4

The SQL itself is invalid for this database

1Check

A prepared statement is parsed by the server when you call prepare(). A wrong table name, column name or syntax fails right there, and mysqli returns false.

mysql -e "SHOW TABLES" my_db
mysql -e "SHOW COLUMNS FROM users" my_db

2Fix

Run the exact SQL string in a MySQL client, fix the name or syntax it complains about, and keep the database name in your connection the same as the one you tested.

$stmt = $mysqli->prepare("SELECT id FROM users WHERE email = ?");
$stmt->bind_param('s', $email);
$stmt->execute();

3Verify

prepare() returns a mysqli_stmt object, and execute() returns true.

php your-script.php

Confirmed by php.net

Did this fix it?

Cause 3 of 4

A ? placeholder is used for a table or column name

1Check

The manual says markers are legal only for values, such as in a VALUES() list or a WHERE comparison, and are not permitted for identifiers such as table or column names.

grep -rn 'FROM ?\|ORDER BY ?\|SET ? ' --include=*.php . | head

2Fix

Bind only values. Pick table or column names from a fixed list in your code and write them into the string.

$allowed = ['name', 'created_at'];
$col = in_array($sort, $allowed, true) ? $sort : 'name';
$stmt = $mysqli->prepare("SELECT id FROM users ORDER BY `$col`");

3Verify

prepare() no longer returns false.

php your-script.php

Confirmed by php.net

Did this fix it?

Cause 4 of 4

The string holds more than one statement

1Check

The manual says the query must consist of a single SQL statement.

grep -rn 'prepare("[^"]*;[^"]*")' --include=*.php . | head

2Fix

Run each statement in its own call. Use query() for the ones without parameters.

$mysqli->query("SET time_zone = '+00:00'");
$stmt = $mysqli->prepare("SELECT id FROM users WHERE email = ?");

3Verify

Both calls succeed.

php your-script.php

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Call mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT) at the start of every request.
  • Bind values only; build names from fixed lists.
  • Test new SQL in a database client first, with the same account the app uses.
  • Keep the number of ? marks equal to the length of the types string in bind_param.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.