0
I need to get the last id inserted, I tried using lasInsertId but I’m not getting it, I don’t know if I ran wrong or so on, but here’s the code of my connection and PDO functions. The error remains in Getultimoid().
//função para fazer conexão com o banco de dados
private function Conectar(){
$options = array(
//trasforma o padrao do banco para utf8
PDO::MYSQL_ATTR_INIT_COMMAND =>"SET NAMES utf8",
//traz algum alerta de erro
PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING
);
$link = new PDO("mysql:host={$this->host};dbname={$this->banco}", $this->user, $this->senha, $options);
return $link;
}
//função que executa a query
function ExecuteSQL($query, array $params = NULL){
$this->obj = $this->Conectar()->prepare($query);
// verificando a quantidade de parametros passados na url
if(@count($params) > 0){
foreach($params as $key => $value){
$this->obj->bindvalue($key, $value);
}
}
return $this->obj->execute();
}
//função para listar os meus elementos
function ListarDados(){
return $this->obj->fetch(PDO::FETCH_ASSOC);
}
//função para mostrar um quantidade de dados
function TotalDados(){
return $this->obj->rowCount();
}
//função que armazena os itens dentro de um array
function GetItens(){
return $this->itens;
}
function GetUltimoID(){
return $this->obj->lastInsertId();
}
Looks like you’re running the method
lastInsertId
on the return of$this->Conectar()->prepare
, which will be an instance ofPDOStatement
. You must call the method in the instance of your connection or transaction.– Woss
If I switch to $this->Connect()->lasInsertId(); It returns me id 0
– Jonas
Because there you are creating a new connection. Try to analyze the code you are writing and understand, in fact, what has been done.
– Woss
And that’s why I’m asking for help, I don’t know how to structure, because I’m learning and in the videos classes I saw he doesn’t explain right, I don’t know how to call this instance
– Jonas