Maaz Zindani

Loading the index…

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

mysqli bind_param fails with a by-reference error

Errormysqli_stmt::bind_param(): Argument #2 could not be passed by reference

Short answer

mysqli_stmt::bind_param binds variables, not values, so every argument after the type string must be a variable. A literal, a function call or an expression cannot be bound. Put the value in a variable first, then bind it. For a list of unknown length, build the type string and spread an array with the ... operator.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the line with bind_param and look at each argument after the first. Anything that is not a plain variable is the problem.

    grep -rn 'bind_param' src/ public/
  2. 2Match what you see, then jump to its fix

    1. 1Code such as $stmt->bind_param('s', 'admin') or bind_param('i', 5)A literal value was passed instead of a variableFix
    2. 2Code such as bind_param('s', md5($pass)) or bind_param('i', $a + 1)A function call or expression was passed as the argumentFix
    3. 3Code that uses call_user_func_array([$stmt, 'bind_param'], $params) with an array of plain values, for a WHERE IN listA dynamic list of values is passed as an array of copiesFix

What does the error look like?

  • mysqli_stmt::bind_param(): Argument #2 could not be passed by reference
  • Notice: Only variables should be passed by reference

Cause 1 of 3

A literal value was passed instead of a variable

1Check

bind_param stores a reference to each variable and reads it when execute() runs. A literal has no storage to reference, and the language manual lists a literal passed by reference as a fatal error.

grep -rnE "bind_param\('[a-z]+', *['0-9]" src/

2Fix

Assign the value to a variable and pass that.

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

3Verify

The statement runs without the error.

php -l src/UserRepo.php

Confirmed by php.net

Did this fix it?

Cause 2 of 3

A function call or expression was passed as the argument

1Check

Only variables, and references returned by a function that is declared to return by reference, can be passed by reference. The result of an ordinary function or an expression cannot.

grep -rnE 'bind_param\([^)]*\(' src/

2Fix

Compute the value on its own line, store it in a variable, then bind the variable.

$hash = hash('sha256', $pass);
$stmt->bind_param('s', $hash);

3Verify

Run the query and check the stored value.

php public/test-insert.php

Confirmed by php.net

Did this fix it?

Cause 3 of 3

A dynamic list of values is passed as an array of copies

1Check

The manual notes that bind_param needs references, whereas call_user_func_array accepts values or references. Build the list with the ... operator instead.

grep -rn 'call_user_func_array' src/

2Fix

Make one placeholder and one type letter per value, then spread the array into bind_param.

$ids = ['DEU', 'POL', 'FRA'];
$types = str_repeat('s', count($ids));
$marks = implode(',', array_fill(0, count($ids), '?'));
$stmt = $mysqli->prepare("SELECT Language FROM CountryLanguage WHERE CountryCode IN ($marks)");
$stmt->bind_param($types, ...$ids);
$stmt->execute();

3Verify

Rows come back for every value in the list.

php public/test-in-list.php

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Treat bind_param like assignment targets: only named variables go in.
  • Keep the number of type letters equal to the number of ? placeholders.
  • Turn on mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT) so errors throw instead of passing silently.

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

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

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