Error saving data using PDO

0

I'm having a problem inserting data into the database using the PDO and I do not know why, as I see everything seems to be right, follow the codes below.

Model class

class User {
    private $id;
    private $nome;

    public setNome($nome){
        $this->nome = $nome;
    }

    public getNome(){
        return $this->nome; 
    }
}

DAO Class

class UsuarioDao implements Dao {
    private $conexao;  

    function __construct(){
        $connection = new Connection();
        $this->conexao = $connection->getConnection();
    }    

    public function insert($user){ 
        try {
            $query = "INSERT INTO usuarios(nome) VALUES(:nome)";
            $this->conexao->prepare($query);
            $this->conexao->bindValue(':nome',$user->getNome(),PDO::PARAM_STR);
            return $this->conexao->execute();    
        }catch(PDOException $e){
             echo $e->getMessage();
        }
    }

Class Instance

require_once('User.class.php');
require_once('UserDao.class.php');

class Teste {
    $user = new User();
    $user->setNome('Gabriel');

    $userDao = new UserDao();
    $userDao->insert($user);         
}

About the error log no message appears, only the 500 error message.

And I'm getting the 500 error every time I run this code, I'd like someone to help me find the error in that code. I started learning PHP now I have more knowledge in JAVA taking advantage of the question do you know any framework ORM type Hibernate for PHP?

    
asked by anonymous 06.02.2016 / 00:48

2 answers

1

Switch

class Teste {
    $user = new User();
    $user->setNome('Gabriel');

    $userDao = new UserDao();
    $userDao->insert($user);         
}

by

class Teste {
    $user = new User();
    $nome = $user->setNome('Gabriel');

    $userDao = new UserDao();
    $userDao->insert($nome);         
}

See if it works, I just introduced a variable, $nome to receive $user->setNome('Gabriel');

    
06.02.2016 / 06:11
1

I was able to resolve the following change:

    public function insert($user){ 
    try {
        $query = "INSERT INTO usuarios(nome) VALUES(:nome)";
        $resultado = $this->conexao->prepare($query);
        $resultado->bindValue(':nome',$user->getNome(),PDO::PARAM_STR);

        return $resultado->execute();    
    }catch(PDOException $e){
         echo $e->getMessage();
    }

I created the variable $ result to receive the query preparation and it worked, thanks to the colleagues who tried to help me.

    
06.02.2016 / 13:03