Вопрос

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.

Это было полезно?

Решение

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);

Другие советы

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top