mysqli bind_param fails with a by-reference error
mysqli_stmt::bind_param(): Argument #2 could not be passed by referenceShort 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.
Fix it fast
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/2Match what you see, then jump to its fix
- 1
Code such as $stmt->bind_param('s', 'admin') or bind_param('i', 5)A literal value was passed instead of a variableFix - 2
Code such as bind_param('s', md5($pass)) or bind_param('i', $a + 1)A function call or expression was passed as the argumentFix - 3
Code 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
- 1
What does the error look like?
mysqli_stmt::bind_param(): Argument #2 could not be passed by referenceNotice: 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.phpConfirmed by php.net
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.phpConfirmed by php.net
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.phpConfirmed by php.net
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
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix