1

I want to catch this error:

Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables

A solution was provided here, but it does not catch the error for some reason.

$rc = $stmt->bind_param('iii', $x, $y, $z);
// bind_param() can fail because the number of parameter doesn't match the placeholders in the statement
// or there's a type conflict(?), or ....
if ( false===$rc ) {
  // again execute() is useless if you can't bind the parameters. Bail out somehow.
  die('bind_param() failed: ' . htmlspecialchars($stmt->error));
}

$rc returns 1 even if the number of bind parameters mismatch...

Matthijs
  • 2,216
  • 4
  • 20
  • 33
AlexR
  • 13
  • 3

1 Answers1

0

First of all, you cannot catch such a Warning with a condition like this. Simply because it is raised on the line with bind_param hence it happens before the checking condition below.

Hence, probably, your confusion - the condition works all right but it is just too late.

I would suggest to catch all errors with a simple error handler. the problem is not specific to mysqli, generally you want to catch all warnings and other errors and handle them uniformly. You can find an example in my article on PHP error reporting:

set_error_handler("myErrorHandler");
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
    error_log("$errstr in $errfile:$errline");
    header('HTTP/1.1 500 Internal Server Error', TRUE, 500);
    readfile("500.html");
    exit;
}

It will do exactly what you wanted in your condition but without the need to write such a condition every time you are preparing an SQL query.

Your Common Sense
  • 152,517
  • 33
  • 193
  • 313