Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving Last Inserted ROWID In PHP/OCI

Is it possible to retrieve the rowid of the last inserted Oracle row in PHP? I was trying:

$statement = oci_parse($conn, "INSERT INTO myTable (...) VALUES ( ...)");
$results = oci_execute($statement);
while($row = oci_fetch_assoc($statement)) {
    $rowid = $row['ROWID'];
}

With no luck. I'm getting the error define not done before fetch or execute and fetch at the fetch line.

like image 597
HugeBob Avatar asked Oct 24 '25 23:10

HugeBob


2 Answers

Declare:

$var = "AAAV1vAAGAAIb4CAAC";

Use:

INSERT INTO myTable (...) VALUES ( ...)
RETURNING RowId INTO :p_val

Bind your variable to a PHP variable:

oci_bind_by_name($statement, ":p_val", $val, 18);
like image 122
user3058627 Avatar answered Oct 26 '25 11:10

user3058627


As the previous answer was not really clear to me because it lacks some important information I will point out a similar approach.

In your SQL statement, add the RETURNING INTOclause.

$statement = oci_parse($conn, "INSERT INTO myTable (...) VALUES ( ...) RETURNING ID INTO :id");

Here ID is the name of the column you want to return. Before executing the $statement, you need to bind a PHP variable to your return value. Here I used $returnId (you don't need to declare it beforehand or assign any default value).

oci_bind_by_name($statement, ":id", $returnId);

Only after binding the variable, the statement can be executed.

$success = oci_execute($statement);

$returnId now has the value of the ID column inserted previously.

like image 32
Tobias Kaufmann Avatar answered Oct 26 '25 12:10

Tobias Kaufmann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!