Get the results inserted

0

I have a stored procedure that stores data in the db, and I want to know if there is any function to get that record that is saved in the database.

$sql = "CALL registrar(?,?,?,?,?,?,?)";
$stm = $this->db->prepare($sql);
$stm->bindParam(1, $this->parametro1);
$stm->bindParam(2, $this->parametro2);
$stm->bindParam(3, $this->parametro3);
$stm->bindParam(4, $this->parametro4);
$stm->bindParam(5, $this->parametro5);
$stm->bindParam(6, $this->parametro6);
$stm->bindParam(7, $this->parametro7);
$stm->execute();

The store procedure executes something like this:

INSERT INTO ingresos (fechaingreso, descripcion, horasalida, visitante, servicio, nodo) VALUES ( concat(parametro1, ' ', parametro2),
parametro3, parametro4, parametro5, parametro6, parametro7);
    
asked by Dum 01.08.2018 в 18:09
source

2 answers

2

I just found a way to do it, and it's using the syntax

SELECT @@identity AS id

Which returns the last id of the last entry in the database, so it should be used right after inserting data.

In my code it was something like that.

$sql = "CALL registrar(?,?,?,?,?,?,?)";
$stm = $this->db->prepare($sql);
$stm->bindParam(1, $this->parametro1);
$stm->bindParam(2, $this->parametro2);
$stm->bindParam(3, $this->parametro3);
$stm->bindParam(4, $this->parametro4);
$stm->bindParam(5, $this->parametro5);
$stm->bindParam(6, $this->parametro6);
$stm->bindParam(7, $this->parametro7);
$stm->execute();

//Aquí se busca el último id ingresado
$sqlid = "SELECT @@identity AS id";
$stmid = $this->db->prepare($sqlid);
$stmid->execute();
var_dump($stmid->fetch(PDO::FETCH_ASSOC));
exit;
    
answered by 01.08.2018 в 18:35
0

With this you would bring that record that you inserted, with the name, or you can use the date that would be something more exact also ...

$sql = "SELECT * FROM ingresos WHERE visitante = ?";
$stm = $this->db->prepare($sql);
$stm->bindParam(1, $this->parametro5);
$stm->execute();

I hope you help Bro ... ReNiceCode ...

    
answered by 01.08.2018 в 18:30