Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:
67
Q:

PHP- PDO Only variables should be passed by reference

Hey guys,

I'm currently encountering an issue while working with PHP's PDO extension, and I'm hoping someone can help me out. I keep getting an error message that says "Only variables should be passed by reference" when I try to execute a PDO statement.

Here's the context: I am working on a web application that allows users to create and edit their profiles. To store and retrieve the user data, I'm using PDO with a MySQL database. Everything was working perfectly fine until I started adding some functionality to update the user's profile.

Here's a snippet of my code:

```php
$stmt = $pdo->prepare("UPDATE users SET email = :email, username = :username WHERE id = :id");
$stmt->bindParam(':email', $email);
$stmt->bindParam(':username', $username);
$stmt->bindParam(':id', $id);
$stmt->execute();
```

The error is triggered at the `$stmt->execute();` line, and I'm not sure what I'm doing wrong. I have double-checked my code and made sure that all the variables (`$email`, `$username`, and `$id`) are properly defined before the execution.

I have tried searching on various forums and found some similar issues related to passing variables by reference in PDO. However, the solutions suggested didn't resolve my problem.

Any insights or suggestions on how to fix this issue would be greatly appreciated. Let me know if you need any additional information.

Thanks in advance!

All Replies

mcorkery

Hey there!

I've encountered a similar issue before, and I believe I can help you out. The error message "Only variables should be passed by reference" often occurs when you try to bind a value using `bindParam()` instead of `bindValue()` in PDO.

In your code snippet, try replacing the `bindParam()` calls with `bindValue()` like this:

php
$stmt = $pdo->prepare("UPDATE users SET email = :email, username = :username WHERE id = :id");
$stmt->bindValue(':email', $email);
$stmt->bindValue(':username', $username);
$stmt->bindValue(':id', $id);
$stmt->execute();


By using `bindValue()`, you pass the values directly instead of passing them by reference like `bindParam()` does. This should resolve the error you are experiencing.

Give it a try and let me know if it works for you. Good luck!

kaycee81

Hey there!

I've had a similar issue in the past and managed to resolve it differently. The error message "Only variables should be passed by reference" can also occur when you attempt to bind a value that is not actually a variable, but rather a function call or an expression.

In your case, ensure that the variables you are passing to `bindParam()` are indeed variables and not the result of a function call or any other expression. If they are not straightforward variables, you can assign them to temporary variables and then bind those.

For example:

php
$emailValue = $email;
$usernameValue = $username;
$idValue = $id;

$stmt = $pdo->prepare("UPDATE users SET email = :email, username = :username WHERE id = :id");
$stmt->bindParam(':email', $emailValue);
$stmt->bindParam(':username', $usernameValue);
$stmt->bindParam(':id', $idValue);
$stmt->execute();


By assigning the values to temporary variables (`$emailValue`, `$usernameValue`, `$idValue`), and then binding those variables, you can avoid the "Only variables should be passed by reference" error.

Give it a shot and see if it works for you. Let us know if you need any further assistance. Good luck!

New to LearnPHP.org Community?

Join the community