Insert php with object as parameter

5

I would like to understand how I can do an insert function in php that uses an object as a parameter. Example:

class Aluno{
   private $nome;
   private $curso;
}

getters e setters...

public function insere(Aluno $aluno){
// suponho que aqui fica a definição dos atributos e o retorno do mysqli_query (não tenho certeza)

}

My question is how can I get this function to insert a record into the database when it is simply called by an instantiated object. And I would like this function to not undergo changes in the parameter, even if you put more attributes in the class. I need it to always be a Student object. I would also like to understand how I can use the return of this function.

    
asked by anonymous 28.11.2015 / 00:54

1 answer

1

Hello, good evening. I'm a beginner in php, and I also do not use mysqli (I'm using PDO ),

$pdo = new PDO("mysql:host=localhost;dbname=nome_banco", "usuario_banco", "senha_banco");
$insert = $pdo->prepare("INSERT INTO NomeTabela (nome,curso) VALUES (:nome,:curso);");
$insert->bindValue(":nome",$aluno->nome);
$insert->bindValue(":curso",$aluno->curso);
$insert->execute();
    
28.11.2015 / 04:47