$conn=oci_connect('hr','welcome','localhost/XE');
if (!$conn) {$e=oci_error();trigger_error(htmlentities($e['message'],ENT_QUOTES),E_USER_ERROR);
}// Find the version of the databasepreg_match('/Release ([0-9]+)\./',oci_server_version($conn),$matches);$oracleversion=$matches[1];// This is the query you want to "page" through$sql='SELECT city, postal_code FROM locations ORDER BY city';
if ($oracleversion>=12) {// Make use of Oracle 12c OFFSET / FETCH NEXT syntax$sql=$sql.' OFFSET :offset ROWS FETCH NEXT :numrows ROWS ONLY';
} else {// Older Oracle versions need a nested query selecting a subset
// from $sql. Or, if the SQL statement is known at development
// time, consider using a row_number() function instead of this
// nested solution. In production environments, be careful to
// avoid SQL Injection issues with concatenation.$sql="SELECT * FROM (SELECT a.*, ROWNUM AS my_rnum
FROM ($sql) a
WHERE ROWNUM <= :offset + :numrows)
WHERE my_rnum > :offset";
}$offset=0;// skip this many rows$numrows=5;// return 5 rows$stid=oci_parse($conn,$sql);oci_bind_by_name($stid,':numrows',$numrows);oci_bind_by_name($stid,':offset',$offset);oci_execute($stid);
while (($row=oci_fetch_array($stid,OCI_ASSOC+OCI_RETURN_NULLS)) !=false) {
echo$row['CITY'] ." ".$row['POSTAL_CODE'] ."
\n";
}// Output is:
// Beijing 190518
// Bern 3095
// Bombay 490231
// Geneva 1730
// Hiroshima 6823oci_free_statement($stid);oci_close($conn);?>