PDO bindParam fails when you give it a value instead of a variable
Cannot pass parameter 2 by referenceShort 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.
Fix it fast
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 -202Match what you see, then jump to its fix
- 1
bindParam(':status', 1, ...), bindParam(':kind', self::KIND) or bindParam(':x', null)A literal or a constant is passed as the valueFix - 2
bindParam(':type', $object->getType()) or bindParam(':d', date('Y-m-d'))A function or method result is passed directlyFix - 3
No PHP error, but every row gets the last value, or an unexpected onebindParam in a loop, or a variable changed after bindingFix
- 1
What does the error look like?
Cannot pass parameter 2 by referencePDOStatement::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
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
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.php2Fix
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
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Back end
CodeIgniter 4: try/catch does not catch a failed insert in the model
DatabaseException
CodeIgniter throws a DatabaseException on a failed query only when DBDebug is true, and inside a transaction it does not throw unless you ask.
3 causes, each with a fixBack end
CodeIgniter 4 where() builds a different condition than you meant
A SQL syntax error that appears only when a value contains a quote character
The query builder joins every where() with AND and every orWhere() with OR, with no brackets.
3 causes, each with a fixBack end
Call to undefined function mysql_pconnect() after a PHP upgrade
Call to undefined function mysql_connect()
The old mysql_* functions belonged to the ext/mysql extension, which PHP removed in version 7.0.
2 causes, each with a fixBack end
CodeIgniter cannot connect to the MySQL database server
Unable to connect to your database server using the provided settings.
CodeIgniter only reports that its connection failed.
4 causes, each with a fixBack end
Laravel insert fails with 'Field user_id doesn't have a default value'
The insert works when the column is removed or made nullable
Error 1364 means Laravel sent an INSERT without a value for a NOT NULL column that has no default, and MySQL strict mode refuses it.
3 causes, each with a fixBack end
Laravel migration cannot drop a column that has a foreign key
Error on rename of '%s' to '%s' (errno: %d - %s)
MySQL refuses to drop a column while a foreign key constraint still uses it.
2 causes, each with a fix