Maaz Zindani

Loading the index…

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

PDO bindParam fails when you give it a value instead of a variable

ErrorCannot pass parameter 2 by reference

Short answer

bindParam binds a variable by reference and reads it when execute() runs. A literal, a constant, null or a function result is not a variable, so PHP refuses it. Pass a real variable, or switch to bindValue, which copies the value at once and accepts any expression.

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

Fix it fast

  1. 1Find out which cause you have

    Find the bindParam call from the stack trace and look at its second argument. If it is anything other than a plain variable, one of the causes below applies.

    grep -rn "bindParam(" src/ app/ | head -20
  2. 2Match what you see, then jump to its fix

    1. 1bindParam(':status', 1, ...), bindParam(':kind', self::KIND) or bindParam(':x', null)A literal or a constant is passed as the valueFix
    2. 2bindParam(':type', $object->getType()) or bindParam(':d', date('Y-m-d'))A function or method result is passed directlyFix
    3. 3No PHP error, but every row gets the last value, or an unexpected onebindParam in a loop, or a variable changed after bindingFix

What does the error look like?

  • Cannot pass parameter 2 by reference
  • PDOStatement::bindParam(): Argument #2 ($var) could not be passed by reference

Cause 1 of 3

A literal or a constant is passed as the value

1Check

The manual declares the second argument as a reference to a variable (mixed &$var). A number, a string, null or a class constant has no variable to point at.

// fails
$stmt->bindParam(':status', 1, PDO::PARAM_INT);

2Fix

Use bindValue when you have a value. It copies the value at the time of the call.

$stmt->bindValue(':status', 1, PDO::PARAM_INT);
$stmt->bindValue(':note', null, PDO::PARAM_NULL);

3Verify

Run the statement. It executes with no error and the row has the value.

$stmt->execute();

Confirmed by php.net

Did this fix it?

Cause 2 of 3

A function or method result is passed directly

1Check

A call returns a value, not a variable, so there is nothing to bind by reference.

grep -rn "bindParam([^,]*,[^$]*(" src/

2Fix

Store the result in a variable first, or use bindValue.

$today = date('Y-m-d');
$stmt->bindParam(':d', $today);

3Verify

Execute the statement and check the stored date.

$stmt->execute();

Confirmed by php.net

Did this fix it?

Cause 3 of 3

bindParam in a loop, or a variable changed after binding

1Check

bindParam reads the variable only when execute() runs, not when you call bindParam. If you bind inside a loop with one variable, or reassign the variable before execute, the later value is sent.

// look for bindParam inside foreach, followed by one execute() after the loop
grep -n "foreach\|bindParam\|execute" src/Repo.php

2Fix

Use bindValue inside loops, or bind once before the loop and assign the variable before every execute().

foreach ($names as $name) {
    $stmt->bindValue(':name', $name);
    $stmt->execute();
}

3Verify

Insert two different names and read both back.

SELECT name FROM people ORDER BY id DESC LIMIT 2;

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Default to bindValue. Use bindParam only when you really want the variable read late, at execute().
  • Put the value in a variable with a clear name first when it is the result of a call.

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

Next step

Want this handled for good?

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