I want to extract the autoincrementable id when inserting the record

0

I need to save the id's in the variable $ last [], but it keeps me empty or zero. The sp_ins_ott_registro () is a sp that inserts in a table of 5 attributes, which are seen there and the AI.

This way it returns zero to me.

            for ($i=0; $i <$contadorArray1; $i++) { 
                $sqlRegistroMuestra="call sp_ins_ott_registro_muestra($ottNumero,'$arrayConPosiciones[$i]',".$arrayConValores[$i].",'".$m1."');";
                $mysqli_obra->query($sqlRegistroMuestra);
                $last[]= $mysqli_obra->insert_id;
                echo "$sqlRegistroMuestra";
            }

From the next one it returns empty

            for ($i=0; $i <$contadorArray1; $i++) { 
                $sqlRegistroMuestra="call sp_ins_ott_registro_muestra($ottNumero,'$arrayConPosiciones[$i]',".$arrayConValores[$i].",'".$m1."');";
                $mysqli_obra->query($sqlRegistroMuestra);
                $last[]= $mysqli_obra->query("select LAST_INSERT_ID()");
                echo "$sqlRegistroMuestra";
            }

This also returns empty

for ($i=0; $i <$contadorArray1; $i++) { 
            $sqlRegistroMuestra="call sp_ins_ott_registro_muestra($ottNumero,'$arrayConPosiciones[$i]',".$arrayConValores[$i].",'".$m1."');";
            $mysqli_obra->query($sqlRegistroMuestra);
            $last[]= mysql_insert_id();
            echo "$sqlRegistroMuestra";
        }
    
asked by Jean Luis Soto Robles 06.11.2018 в 07:04
source

1 answer

0

To return the last id that you have inserted you need to add the following line in your stored procedure sp_ins_ott_registro_muestra :

//Imaginando que haces un insert de un nombre de usuario
INSERT INTO Persons (FirstName) VALUES ('Jean');

//Una vez insertado recuperamos la id
SELECT ID AS LastID FROM Persons WHERE ID = @@Identity;

You'll have to adapt the code since you do not see it, but I hope the idea is clear to you.

    
answered by 06.11.2018 в 10:25