Insert data (ID) in oracle from PHP

0

I'm trying to insert data into an Oracle database.

The problem is in the ID of the table. In mysql it is done with auto_increment but from oracle with sequences. This is my insert code:

$stid = oci_parse($conn, 'INSERT INTO usuarios (iduser, nombre, apellido,correo,passwd,codigo,activacion) VALUES(:iduser, :nombre, :apellido, :email, :passwd, :codigo, :activacion)');

        $activacion = 1;
        $id = $stid->lastInsertId();

        oci_bind_by_name($stid, ':iduser', $id);
        oci_bind_by_name($stid, ':nombre', $user);
        oci_bind_by_name($stid, ':apellido', $apellido);
        oci_bind_by_name($stid, ':email', $email);
        oci_bind_by_name($stid, ':passwd', $pass);
        oci_bind_by_name($stid, ':codigo', $aleatorio);
        oci_bind_by_name($stid, ':activacion', $activacion);

        $r = oci_execute($stid);



        oci_free_statement($stid);
        oci_close($conn); 

The problem is here:

$id = $stid->lastInsertId();

Fatal error: Uncaught Error: Call to a member function lastInsertId () on resource in

How can I get the last id from the table, add one and insert the value? Thank you very much.

    
asked by francisco 16.05.2018 в 00:53
source

3 answers

0

The best way to do it is to create a sequence and work with it. This way of working is much more efficient than looking for the last ID inserted and adding one.

    
answered by 16.05.2018 в 11:13
0

The best way to do it is:

$id = mysqli_insert_id($conexionbasededatos);

Your example would be like this:

$stid = oci_parse($conn, 'INSERT INTO usuarios (iduser, nombre, apellido,correo,passwd,codigo,activacion) VALUES(:iduser, :nombre, :apellido, :email, :passwd, :codigo, :activacion)');

$activacion = 1;
$id = mysqli_insert_id($conn); //solo he cambiado esta linea

oci_bind_by_name($stid, ':iduser', $id);
oci_bind_by_name($stid, ':nombre', $user);
oci_bind_by_name($stid, ':apellido', $apellido);
oci_bind_by_name($stid, ':email', $email);
oci_bind_by_name($stid, ':passwd', $pass);
oci_bind_by_name($stid, ':codigo', $aleatorio);
oci_bind_by_name($stid, ':activacion', $activacion);

$r = oci_execute($stid);



oci_free_statement($stid);
oci_close($conn);
    
answered by 13.12.2018 в 17:09
-1

In Oracle it does not exist:

$id = $conn->lastInsertId();

You must do it with a select max(id) from table . and there you show it.

    
answered by 16.05.2018 в 01:21