Passing a variable my reference in PDO

I’m brand new here and still trying to find my way around. I first posted this as a message but I kept seeing that I was sending it to myself. I’m new, and I did read the how to article and I’m still kind of confused about how to get the best of this forum. Getting to the point: I’m trying to get a better understanding of Fatal error: Cannot pass parameter 2 by reference . I saw the post on it but it didn’t address my particular issue. The post that I read had multiple parameters. My problem only has two (2). This is the code $stmt->bindParam(1, ‘user_name’); At first this code worked, no errors. Then all of a sudden I started getting the error. I understand passing my reference to be putting a & in front of a variable. I haven’t done that. That’s why I’m confused. Can anyone shed some light of this problem for me and point me in the right direction. I’d appreciate it. Thanks.

Can you display the method $stmt->bindParam(1, 'user_name')

It looks as though the method’s second parameter is expecting a value to be passed by reference and a string is being passed instead of a value.

[off topic]
I seldom pass by reference and far prefer returning a $result from a function or method.

On the few occasions where a value is used as pass by reference I capitalize the value and prefix with $BYREF
[/off topic]

A good source of PHP is their online manual.

$stmt->bindParam(1, 'user_name');

this code will always throw an error, as you cannot use a constant value with bindParam.

Instead of binding, send all values right into execute, like

$stmt = $pdo->prepare(...);
$stmt->execute(['user_name','another value']);
1 Like

for that you’d need bindValue.

I appreciate the replies I got. Last night when I left the forum I went back to my code in question and changed bindParam(1, ‘user_name’) to bindValue(1,‘user_name’) and the error went away. Therefore this is solved. Thanks to everybody.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.