Insert error in database using PDO: "Only variables should be passed by Reference"

Asked

Viewed 138 times

1

I am passing input parameters in the database using PDO and O, but when giving the Insert command using the parameters $stm->bindParam(1, $usuario->getnivel()); it returns error saying that I must pass only variables for insertion.

Follows code:

  public function cadastrar(Cadastro $usuario) {
        $stm = $this->pdo->prepare('INSERT INTO usuarios (nivel,username,senha) VALUES (?,?,?)');   
        $stm->bindParam(1, $usuario->getnivel());
        $stm->bindParam(2, $usuario->getusername());
        $stm->bindParam(3, $usuario->getsenha());
        $stm->execute();
}
  • Always put exactly the error that appears, the problem here had nothing to do with orientation to objects or mysql, in case the error is this: Only variables should be passed by Ference, please take it as a constructive tip.

  • Okay, thank you, I’ll take this detail when possible!

1 answer

1


bindParam() does not accept values or method returns only accept variables or constants, in which case it is best to exchange for bindValue()

$stm->bindValue(1, $usuario->getnivel());
$stm->bindValue(2, $usuario->getusername());
$stm->bindValue(3, $usuario->getsenha());

More details on: What is the difference between bindParam and bindValue?

  • Thanks Dude, the bindValue worked was just that. Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.