Binding MySQLi Parameters in PHP 8.1

Content:

PHP 8.1 introduces a new way to bind parameters to a MySQLi prepared statement.

Prior to PHP 8.1, there are several steps required to bind your parameters. Not only do the parameters need to be bound using a call to bind_param(), you must also specify a data type for each parameter.

It’s common to see code similar to this:

$stmt->bind_param(str_repeat('s', count($values)), ...$values);
$stmt->execute();

PHP 8.1 introduces several changes, which simplify this process.

Rather than using bind_param(), an array of parameters can now be passed directly to execute().

$stmt->execute([$values]);

It’s not necessary to specify a per-parameter data type when using this method.

This simplifies the handling of parameters in MySQLi, bringing it in line with the way ODBC handles prepared statement parameters.

If you like what we do, consider supporting us on Ko-fi