To select last record, I was using this when my database was MySQL:
$result = mysql_query("SELECT Id
FROM test
ORDER BY LENGTH(Id), Id ASC");
$count = mysql_numrows($result);
if($count != 0) {
$lastid = mysql_result($result,$count-1,"Id");
}
…and it works fine.
But now my table is in an Oracle database – I wrote:
$stid = oci_parse($conn, "SELECT Id
FROM test
ORDER BY LENGTH(Id), Id ASC");
oci_execute($stid);
$p = oci_parse($conn, "SELECT COUNT(ID)
FROM test");
oci_execute($p);
$count = oci_fetch_array($p);
if($count[0] != 0) {
$lastid = oci_result($stid, $count[0]-1);
}
It doesn’t work – I am not getting the last record.
If you read the documentation for oci_result, you’ll find you’re using the second parameter incorrectly.
The second parameter is what you use to retrieve the column – it can be the column name, or the ordinal value. Ordinal value is a number, starting at one (1) based on the columns specified in the SELECT clause. Ordinals aren’t a recommended practice, because if the query changes — if you forget to update the ordinal reference your retrieval of values is screwed. Effectively, the subsequent COUNT query returns a value higher than the number of columns in the first queries SELECT clause.
oci_resultlacks the third parameter thatmysql_resultprovides.To make things work in Oracle, use:
The updated query will return one row, the latest based on reversing the
ORDER BYyou were using previously.