1

I have this php function to add an object to the database, but it is giving me some troubles to make it work. I'm pretty used to the java way, so i tried to follow the same pattern, and I'm getting an error message that says "Strict standards: Only variables should be passed by reference". This is the code:

public function saveUser(User $user){
    $this->connection = DaoFactory::openConnection();
    $this->query = $this->connection->prepare($this->SAVE_QUERY);

    $this->query->bindParam(':name', $user->getName());
    $this->query->bindParam(':lastName', $user->getLastName());
    $this->query->bindParam(':age', $user->getAge());
    $this->query->bindParam('gender', $user->getGender());
    $this->query->bindParam(':email', $user->getEmail());
    $this->query->bindParam(':password', $user->getPassword());

    $this->query->execute();
    $this->connection = null;

}

I've searched and i found that the object attribute should be placed in a variable to avoid this issue, but by doing so, the code gets very messy and complex.Example:

$name = $user->getName();
$this->query->bindParam(':name',$name);

Is there any other way to solve this?

Gabriel Dzul
  • 158
  • 1
  • 5

1 Answers1

2

bindParam expects a variable passed by reference, you're instead giving it a value returned from a function, which cannot be passed by reference. So instead, use the other API method bindValue() to bind your values.

See https://stackoverflow.com/a/14413428/476 for what the difference is.

Community
  • 1
  • 1
deceze
  • 510,633
  • 85
  • 743
  • 889