php-src icon indicating copy to clipboard operation
php-src copied to clipboard

mysqli_stmt_bind_param Fatal error with MYSQLI_REPORT_OFF set

Open Madpeterz opened this issue 4 years ago • 3 comments

Description

The following code:

<?php

mysqli_report(MYSQLI_REPORT_OFF);
$mysqli_report = mysqli_init();
mysqli_real_connect($mysqli_report, "localhost", "example", "example", "example");

try {
    $sql = "SELECT * FROM `example` WHERE cvalue = ? OR cvalue = ?";
    $stmt = $mysqli_report->prepare($sql);
    if ($stmt === false) {
        $error_msg = "unable to prepair: " . $mysqli_report->error;
        die($error_msg);
    }
    $bind_text = "ii";
    $bind_args = [13];
    $result = mysqli_stmt_bind_param($stmt, $bind_text, ...$bind_args);
    die("This should not run: ".json_encode($result));
} catch (Exception $e) {
    error_log("in error log: " . $e->getMessage());
}

Resulted in this output:

PHP Fatal error:  Uncaught ArgumentCountError: The number of elements in the type definition string must match the number of bind variables in mysqli.php:16
Stack trace:
#0 mysqli.php(16): mysqli_stmt_bind_param(Object(mysqli_stmt), 'ii', 13)
#1 {main}
  thrown in mysqli.php on line 16

Fatal error: Uncaught ArgumentCountError: The number of elements in the type definition string must match the number of bind variables in mysqli.php:16
Stack trace:
#0 mysqli.php(16): mysqli_stmt_bind_param(Object(mysqli_stmt), 'ii', 13)
#1 {main}
  thrown in mysqli.php on line 16

But I expected this output instead:

in error log: The number of elements in the type definition string must match the number of bind variables

PHP Version

PHP 8.1.4 (cli) (built: Mar 16 2022 09:33:31)

Operating System

Windows 11

Madpeterz avatar Apr 10 '22 22:04 Madpeterz

ArgumentCountError is a type of Error, not a type of Exception (uppercase 'E').

If you want to catch any type of exception (lowercase 'e') then look for a Throwable.

damianwadley avatar Apr 10 '22 23:04 damianwadley

Yep my bad still weird thou I expected the fatal error to be suppressed. $result to be false. and the error given over: $stmt->error_list

Madpeterz avatar Apr 10 '22 23:04 Madpeterz

mysqli_report() affects things that happen with MySQL and the database, like invalid queries or connection problems. The ArgumentCountError isn't one of those: it's about you calling the function with the wrong number of arguments. The same thing happens calling just about any other PHP function incorrectly too.

damianwadley avatar Apr 11 '22 00:04 damianwadley